RESTful web service returns non json xml

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

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

But when I make a GET in the violin of the same URI, I get

 "Model: CVS-1679" 

Only under the textview tab.

Why does it return in XML (in the browser and text in Fiddler) and not JSON when I set my ResponseFormat as JSON?

My code is:

 [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; } } 
+5
json c # rest wcf
source share
2 answers

The ASP.NET web service returns the default XML / SOAP message. If you want to return a Json string, you will need to decorate the web service with the [ScriptService] attribute. This tells IIS that this service will be used by ASP.NET AJAX calls. These attributes are part of System.Web.Extensions.

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

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

However, even after the web service and the web method are formatted with this attribute, the response can still be in XML format. This behavior occurs when the client that makes the request does not set the content type of the request header to "application / json".

Before returning, the method call from webmethod is serialized to a Json string using JavaScriptSerializer

Debugging a WebService Using Fiddler

It is very easy to use a violinist to test webservice. The next digit is an example of a Webservice call that returns a json string. Note that the request content type is set to application / json. The parameters expected by webserivce are referenced in the Request Body section. enter image description here

Note that the request content type is set to application / json.

+5
source share

It returns to Json if you look at the format of the data you receive ...

 key: value 

or in your case

 string Model = "CVS-1679" 

When you look at it in the violinist, you see that serialization is serializing from one MS endpoint to another. The elements of serialization and de-serialization in the .NET platform take care of transportation throughout the wire, so when you return the object back to your .NET application at the calling end, you get a variable called "Model" with the value that you expect.

If you try to send the whole class, you will see a lot of nested XML tags, but when you get the object in your code, you will see a first-class citizen in the hierarchy of objects.

The reason it appears in your browser is because the browser doesn't know how to de-serialize it, and therefore just displays the text

+4
source share

All Articles