【问题标题】:How to create WebService that returns data in JSON如何创建以 JSON 格式返回数据的 WebService
【发布时间】:2015-09-26 21:50:58
【问题描述】:

你好,我想问一下如何在没有 XML 标记的 Visual Studio (ASP.NET) 中让 web 服务返回 JSON?

   <string xmlns="http://tempuri.org/">{"account":[{"name":"XXXX ","phoneNumber":"5555544 ","email":"sas@sa.com"},{"name":"slh ","phoneNumber":"0565555","email":"1 "}]}

【问题讨论】:

  • 为什么不用webapi restful?
  • 任何 MVC 控制器(或 webAPIController)都可以为您做到这一点
  • 使用 json 格式的 Web api。
  • 谢谢,但我对 API 没有任何了解

标签: c# asp.net json web-services


【解决方案1】:

要让 WCF REST 服务 (.svc) 返回 JSON,您需要在合同上添加属性,告诉它返回 JSON。请注意,在下面的示例中,响应格式设置为 JSON。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getEmployee/{id}")]
    Employee GetEmployee(int id);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2020-03-14
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多