我创建了一个带有代码隐藏文件的 ASMX 文件。它工作正常,但它正在输出 XML。
但是,我需要它来输出 JSON。 ResponseFormat 配置似乎不起作用。我的代码隐藏是:
[System.Web.Script.Services.ScriptService]
public class _default : System.Web.Services.WebService {
[WebMethod]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public string[] UserDetails()
{
return new string[] { "abc", "def" };
}
}
最佳答案
要接收纯 JSON 字符串,而不将其包装到 XML 中,您必须将 JSON 字符串直接写入 HttpResponse
并将 WebMethod
返回类型更改为无效
.
[System.Web.Script.Services.ScriptService]
public class WebServiceClass : System.Web.Services.WebService {
[WebMethod]
public void WebMethodName()
{
HttpContext.Current.Response.Write("{property: value}");
}
}
关于c# - 如何让 ASMX 文件输出 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/211348/