2017-02-07 26 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"/>