I’ve got a simple ASP.NET webservice. I’m wanting to return a string of json as the result. By default, my webservice is wrapping 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 any way i can make my webservice NOT return some xml, but just write my result as raw output?
- Can I define the HTTP-StatusCode in the webservice? eg.
200, 201, 202, 404, 500, etc? - Can i define the response type? eg.
application/json
Cheers!
You can do this fairly easily by creating a .ashx handler instead of a normal web service – but at that point you lose a lot of the infrastructure around web services, in particular anything interpreting the data coming from the client in the structured way that SOAP gives you.
Here’s an example of creating an RSS feed as a “raw” handler, and here’s a more general tutorial. It’s not particularly tricky – if those don’t help you much, do a search for ashx and you’ll get lots of hits.
I don’t know how easy it is to do from a web service project though – I’ve only done it from a straight ASP.NET web application project. It may well “just work” though – it’s worth a try.