How can I stop the ASP.NET web service from returning xml data?

I have a simple ASP.NET web service. As a result, I want to return a json string. By default, my web service wraps my json result in some xml.

eg.

<?xml version="1.0" encoding="utf-8"?> <string xmlns="http://webservice.streetadvisor.com/">{.... json result in here ... }</string> 

Booo.

  • Is there a way to make my web service NOT return some xml, but just write my result as the original output?
  • Can I define an HTTP-StatusCode in a web service? eg. 200, 201, 202, 404, 500 , etc.
  • Can I determine the type of response? eg. application/json

Hooray!

+4
source share
3 answers

You can do this quite easily by creating an .ashx handler instead of a regular web service, but at this point you are losing a lot of infrastructure around web services, in particular, something that interprets the data coming from the client in a structured way that SOAP gives you.

Here is an example of creating an RSS feed as a raw handler, and here is a more general tutorial . This is not particularly difficult - if this does not help you, do an ashx search and you will get many hits.

I don’t know how easy it is to do this from a web service project, but I did it only from a simple ASP.NET web application project. Perhaps this β€œjust works” - it's worth a try.

+3
source

Sometimes, if they want to return JSON, I just use the .aspx page with:

 Response.Clear(); Response.ContentType = "text/javascript"; 

Then, using the .NET Json environment (e.g. 1 here ), I create json, I want it to return.

+2
source

You can just use

 [WebMethod] [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 

It worked for me.

0
source

All Articles