RESTful web service returning XML not JSON

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>

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

"Model: CVS-1679"

Only under the textview tab.

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?

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;
            }
        }

Answers


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.

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

[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”.

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

Debugging WebService using Fiddler

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.

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


Need Your Help

Configuring a Silex-Application with paths using Symfony\Component\DependencyInjection

php symfony1 dependency-injection silex

I am currently refactoring a Silex\Application that has grown bigger than initially expected. As part of this work I am moving service definitions from the source into a services.yml and just call a

change div background color on shift + click (javascript)?

javascript html onclick click shift

I already have onclick event for changing border color of a div on mouse click.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.