2017-02-07 31 views
0

在C#WebApi中,嘗試以json格式接收阿拉伯文數據(通過肥皂UI發送),我收到「???」而不是實際的阿拉伯文字。C#RESTful WebApi在json中使用阿拉伯語發佈數據但接收?

網址:

http://localhost:4321/receive/message

JSON格式(請求):

{ 
    "message_no";"123", 
    "user_id":"a123", 
    "text":"أهلا بك", 
} 

型號:

public class MessageBody 
{ 
     [JsonProperty(PropertyName = "message_no")] 
     public string MessageNo { get; set; } 
     [JsonProperty(PropertyName = "user_id")] 
     public int UserId { get; set; } 
     [JsonProperty(PropertyName = "text")] 
     public int Text { get; set; } 
} 

內容接收:

MessageNo:123

用戶ID:A123

文本:??????

+0

在服務器端和客戶端都使用'UTF-8'編碼器嗎? –

+0

現在我沒有使用任何編碼器,您能提供關於如何在服務器端使用'UTF-8'編碼的示例代碼嗎? – FaizanRabbani

+1

當你從chrome瀏覽器打開這個URL時,你看到了什麼?(假設這是一個GET請求)? – Developer

回答

0

我想你已經在服務器端從UTF-8傳輸了字節,任何客戶端代碼也可以使用各種字符串編碼格式來顯示測試。

0

將您的字符串傳遞給此方法。這將返回所需的文本。

public string DecodeEncodedNonAsciiCharacters(string value) 
      { 
       return Regex.Replace(
        value, 
        @"\\u(?<Value>[a-zA-Z0-9]{4})", 
        m => 
        { 
         return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString(); 
        }); 
      } 
0

對於客戶端:

只是使用HTML頁面發佈數據。

除了保存文件的UTF-8格式。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<meta charset="utf-8" /> 

對於服務器端:

下面添加到web.config並使其在<system.web>元素。

<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8"/>