代码之家  ›  专栏  ›  技术社区  ›  Ron Klein Noa Kuperberg

NET-web方法的响应可以是SOAP(XML)和JSON吗?

  •  0
  • Ron Klein Noa Kuperberg  · 技术社区  · 15 年前

    我有一个ASP.NET web应用程序。

    所有这些web方法都基于默认设置。例如:

    using System.Web.Services;
    
    namespace WebApplication2
    {
        [WebService(Namespace = "http://mydomain.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        public class WebService1 : WebService
        {
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
            [WebMethod]
            public Person GetPersonById(int id)
            {
                Person result = new Person();
                // code...
                return person;
            }
        }
    }
    

    响应是SOAP(XML)格式的。

    我的问题是:我可以根据输入参数或头将响应的格式更改为JSON吗?

    3 回复  |  直到 15 年前
        1
  •  0
  •   RPM1984    15 年前

    a的响应类型 ASMX Web服务 由 ResponseFormat

    例如:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public Person GetPersonById(int id)
    

    或

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Person GetPersonById(int id)
    

    所以答案是 (来自一种方法)。

    我相信你可以做一些黑客,但这是推荐的方法。

    在这些技术中 被叫方

    获取: http://api.yourdomain.com/person/1?format=json

    获取: http://api.yourdomain.com/person/1?format=xml

    另外,JSON web服务调用应该是 HTTP帖子 ,出于安全原因被“咕噜”提到 here

        2
  •  0
  •   Community Mohan Dere    9 年前

    你应该去看看Windows通讯基金会(WCF)。使用它,您可以为一个服务定义多个端点。例如,这些端点中的每一个都可以以不同的格式返回数据。

    REST / SOAP endpoints for a WCF service

        3
  •  0
  •   Shiv Kumar    15 年前