如何在没有XML的情况下接受SOAP WS中的JSON

时间:2016-03-07 10:40:51

标签: c# json xml web-services soap

我在.Net 3.5版本中编写了一个带有SOAP的WEBSERVICE。当您在IE上显示ASMX文件时,它会显示以下信息。

我告诉我的客户我可以接受他们发给我的JSON对象。我接受一个令牌(字符串)和param1(JSON对象)。但是,令牌和param1仍然在XML中发送。顾客告诉我,他只想给我发送json。我怎么能以他想要的方式改变它?谁能告诉我该怎么做?

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/PaymentRequest"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PaymentRequest xmlns="http://tempuri.org/">
      <guid>string</guid>
      <param1>
        <organisationId>string</organisationId>
        <kdv>decimal</kdv>
        <bsmv>decimal</bsmv>
        <payment>
          <paymentId>string</paymentId>
          <signedDate>dateTime</signedDate>
          <amount>decimal</amount>
          <installment>int</installment>
          <appliedRate>decimal</appliedRate>
          <cardId>string</cardId>
        </payment>
      </param1>
    </PaymentRequest>
  </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:1)

1-)您可以在[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

之上添加服务

喜欢这个

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
         JavaScriptSerializer js = new JavaScriptSerializer();
         Context.Response.Clear();
         Context.Response.ContentType = "application/json";           
         Context.Response.Write(js.Serialize("HelloWorld"));
    }

2-)添加你的web.config这个处理程序部分

configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.webServer>
        <handlers>
            <add name="ScriptHandlerFactory"
                 verb="*" path="*.asmx"
                 type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 resourceType="Unspecified" />
        </handlers>
    </system.webServer>
</configuration>