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
我有这个简单的Web服务,现在它只是查看部件号是否为A123456789然后它返回一个型号。这将由将连接到数据库以检查partno的逻辑替换,然后返回实际的型号。但此时我只需要它返回一些虚拟JSON数据。然而,当我使用Fiddler并查看http:// localhost:PORT / Scan / Model / A123456789的web broswer中的调用时,它返回此
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Model: CVS-1679</string>
But when I do a GET in fiddler of the same URI I get
但是当我在同一个URI的fiddler中进行GET时,我得到了
"Model: CVS-1679"
Only under the textview tab.
仅在textview选项卡下。
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?
当我将ResponseFormat设置为JSON时,为什么它以XML(在Fiddler中的浏览器和文本中)而不是JSON返回?
My Code:
[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;
}
}
2 个解决方案
#1
5
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.
ASP.NET webservice默认返回XML / SOAP消息。如果要返回Json字符串,则需要使用[ScriptService]属性修饰Webservice。这告诉IIS,ASP.NET AJAX调用将使用此服务。这些属性是System.Web.Extensions的一部分。
You can define the web method response format by decorating the webmethod with ScriptMethod attribute.
您可以通过使用ScriptMethod属性修饰web方法来定义Web方法响应格式。
[ScriptMethod(ResponseFormat = ResponseFormat.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”.
但是,即使在通过这些属性修改webservice和webmethod之后,响应仍然可以是XML格式。当发出请求的客户端未将Request Header内容类型设置为“application / json”时,会发生此行为。
Before return the method call from webmethod serialize to Json string using JavaScriptSerializer
在使用JavaScriptSerializer将方法调用从webmethod serialize返回到Json字符串之前
Debugging WebService using Fiddler
使用Fiddler调试WebService
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.
使用fiddler测试webservice非常容易。下图是如何调用返回json字符串的Webservice的示例。请注意,请求内容类型设置为application / json。 webserivce期望的参数在Request Body部分中进行了测量。
Note that the request content type is set to application/json.
请注意,请求内容类型设置为application / json。
#2
4
It is being returned in Json if you look at the format of the data you get...
如果你看一下你得到的数据的格式,它将在Json中返回...
key: value
or in your case
或者在你的情况下
string Model = "CVS-1679"
When you view it in fiddler your seeing the raw serialization transport from one MS endpoint to the other. The serialisation & De-serialisation elements in the .NET framework take care of transporting it across the wire, so that when you get the object back into your .NET app at the calling end, you get a variable called Model with the value you expect.
当您在fiddler中查看它时,您会看到从一个MS端点到另一个MS端点的原始序列化传输。 .NET框架中的序列化和反序列化元素负责通过线路传输它,因此当您将对象返回到调用端的.NET应用程序时,您将获得一个名为Model的变量,其中包含您期望的值。
If you try to send an entire class you'll see a lot of nested XML tags, but when you get the object in your code, you'll see a first class citizen in the object hierarchy.
如果您尝试发送整个类,您将看到许多嵌套的XML标记,但是当您在代码中获得该对象时,您将在对象层次结构中看到第一类公民。
The reason it appears in your browser is because, the browser doesn't know how to de-serialise it, and so just displays the text
它出现在浏览器中的原因是,浏览器不知道如何对其进行反序列化,因此只显示文本
#1
5
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.
ASP.NET webservice默认返回XML / SOAP消息。如果要返回Json字符串,则需要使用[ScriptService]属性修饰Webservice。这告诉IIS,ASP.NET AJAX调用将使用此服务。这些属性是System.Web.Extensions的一部分。
You can define the web method response format by decorating the webmethod with ScriptMethod attribute.
您可以通过使用ScriptMethod属性修饰web方法来定义Web方法响应格式。
[ScriptMethod(ResponseFormat = ResponseFormat.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”.
但是,即使在通过这些属性修改webservice和webmethod之后,响应仍然可以是XML格式。当发出请求的客户端未将Request Header内容类型设置为“application / json”时,会发生此行为。
Before return the method call from webmethod serialize to Json string using JavaScriptSerializer
在使用JavaScriptSerializer将方法调用从webmethod serialize返回到Json字符串之前
Debugging WebService using Fiddler
使用Fiddler调试WebService
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.
使用fiddler测试webservice非常容易。下图是如何调用返回json字符串的Webservice的示例。请注意,请求内容类型设置为application / json。 webserivce期望的参数在Request Body部分中进行了测量。
Note that the request content type is set to application/json.
请注意,请求内容类型设置为application / json。
#2
4
It is being returned in Json if you look at the format of the data you get...
如果你看一下你得到的数据的格式,它将在Json中返回...
key: value
or in your case
或者在你的情况下
string Model = "CVS-1679"
When you view it in fiddler your seeing the raw serialization transport from one MS endpoint to the other. The serialisation & De-serialisation elements in the .NET framework take care of transporting it across the wire, so that when you get the object back into your .NET app at the calling end, you get a variable called Model with the value you expect.
当您在fiddler中查看它时,您会看到从一个MS端点到另一个MS端点的原始序列化传输。 .NET框架中的序列化和反序列化元素负责通过线路传输它,因此当您将对象返回到调用端的.NET应用程序时,您将获得一个名为Model的变量,其中包含您期望的值。
If you try to send an entire class you'll see a lot of nested XML tags, but when you get the object in your code, you'll see a first class citizen in the object hierarchy.
如果您尝试发送整个类,您将看到许多嵌套的XML标记,但是当您在代码中获得该对象时,您将在对象层次结构中看到第一类公民。
The reason it appears in your browser is because, the browser doesn't know how to de-serialise it, and so just displays the text
它出现在浏览器中的原因是,浏览器不知道如何对其进行反序列化,因此只显示文本