RESTful Web服务返回XML而不是JSON [英] RESTful web service returning XML not JSON

查看:592
本文介绍了RESTful Web服务返回XML而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的Web服务,现在它只是看零件号是否为A123456789,然后返回型号.这将由连接到数据库中的逻辑所代替,以检查零件编号,然后返回实际的型号.但是在这一点上,我只需要它返回一些虚拟JSON数据.但是,当我使用Fiddler并查看 http://localhost:PORT/Scan/型号/A123456789 它返回此值

I have this simple web service, right now it just looks to see if the part number is A123456789 and then it returns a model number. This will be replaced by logic that will be connecting into a database to check the partno against and then return the actual model number. But at this point I just need it to return some dummy JSON data. However when I use Fiddler and look at the call in the web broswer of http://localhost:PORT/Scan/Model/A123456789 it returns this

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Model: CVS-1679</string>

但是当我在相同URI的提琴手中执行GET操作时,会得到

But when I do a GET in fiddler of the same URI I get

"Model: CVS-1679"

仅在textview标签下.

Only under the textview tab.

当我将ResponseFormat设置为JSON时,为什么要以XML(在浏览器中以及Fiddler中的文本)而不是JSON中返回它?

Why is it being returned in XML (in the browser and text in Fiddler) and not JSON, when I have setup my ResponseFormat to be JSON?

我的代码:

[WebGet(UriTemplate = "Model/{partno}", ResponseFormat = WebMessageFormat.Json)]
        public string Model(string partno)
        {
            if (partno == "A123456789")
            {
                string modelno = "CVS-1679";
                return "Model: " + modelno;
            }
            else
            {
                string modelno = "CVS-1601";
                return "Model: " + modelno;
            }
        }

推荐答案

ASP.NET Web服务默认情况下返回XML/SOAP消息.如果要返回Json字符串,则需要使用[ScriptService]属性装饰Webservice.这通知IIS,ASP.NET AJAX调用将使用此服务.这些属性是System.Web.Extensions的一部分.

ASP.NET webservice return XML / SOAP message by default. In case you want to return Json string, you would need to decorate the Webservice with [ScriptService] attribute. This inform the IIS that this service would be used by ASP.NET AJAX calls. These attribute are part of System.Web.Extensions.

您可以通过使用ScriptMethod属性装饰Web方法来定义Web方法响应格式.

You can define the web method response format by decorating the webmethod with ScriptMethod attribute.

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

但是,即使通过这些属性修饰了webservice和webmethod之后,响应仍然可以是XML格式.当发出请求的客户端未将请求标头内容类型设置为"application/json"时,就会发生此行为.

However even after decorating the webservice and webmethod by these attribute, the response can still be in XML format. This behaviour happen when the client which makes the request does not set the Request Header content type as "application/json".

在使用JavaScriptSerializer将来自webmethod的方法调用序列化为Json字符串之前,

Before return the method call from webmethod serialize to Json string using JavaScriptSerializer

使用Fiddler调试WebService

使用提琴手来测试Web服务非常容易.下图是如何调用返回json字符串的Web服务的示例. 请注意,请求内容类型设置为application/json.在请求正文部分中提及了webserivce期望的参数.

It is quite easy to use fiddler to test webservice. Following figure is an example of how to call a Webservice which returns a json string. Note that the request content type is set to application/json. The parameters expected by webserivce is mentioed in the Request Body section.

请注意,请求内容类型设置为application/json.

这篇关于RESTful Web服务返回XML而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆