c# - 通过 ASP.NET JSON 返回阿拉伯文本

标签 c# asp.net json arabic

我想通过以 JSON 形式返回的 asp.net 网络方法返回阿拉伯文本, 这是我正在使用的代码:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getAr()
{
    ResultTemplate resultTemplate = new ResultTemplate();
    resultTemplate.data = "بسم الله";

    JavaScriptSerializer js = new JavaScriptSerializer();
    string retJSON = js.Serialize(resultTemplate);

    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.AddHeader("content-length", retJSON.Length.ToString());
    Context.Response.Flush();
    Context.Response.Write(retJSON);
}

当我运行网络方法时,它一直在加载而没有任何响应!

最佳答案

您不应该像这样使用 WebMethod。将其签名更改为:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getAr()
{
  string result;
  // fill it ...
  return result;
}

加上

Response.Flush 表示立即发送缓冲输出。所以你应该把它放在最后。

Context.Response.Write(retJSON);
Context.Response.Flush();

此外,content-length 不等于 string.Length。您应该将其转换为字节数组,然后使用其长度。

byte[] s = Encoding.UTF8.GetBytes(resultString);
response.AddHeader("Content-Length", s.Length.ToString());
response.BinaryWrite(s);

关于c# - 通过 ASP.NET JSON 返回阿拉伯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086586/

相关文章:

c# - 如何确定引用 Microsoft.VC90.DebugCRT 的内容?

c# - EF4 两个模型一个数据库,可能吗?

c# - 将 DataRow 中的字符串转换为 double

asp.net - 点击回车时,文本框会聚焦在按钮上吗?

javascript - 向现有 JSON 对象添加新元素

json - Golang 空类型和 json.Decode()

c# - ViewData、ViewBag 和 TempData 是否违反了 MVC?

c# - 创建通用扩展方法以提供过滤功能

c# - 将 IEnumerable 动态转换为 IQueryable 或使用 LINQ 表达式动态调用 AsQueryable

json - Flutter & SurveyKit - 如何将 SurveyResult 序列化为 JSON