views:

429

answers:

3

Hi folks,

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/"&gt;{.... 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!

+3  A: 

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.

Jon Skeet
I was afraid this was going to be the answer :) So then I would have to access the querystring or forms directly, for the GET or POST arguments?
Pure.Krome
Yup - it that likely to be a problem for you? Do you really have something like SOAP as an input but expect something radically different as output? What is the client here?
Jon Skeet
Nope. not a problem. just a bit more plumbing (which i've started doing, an hour or so ago). I'm used to RESTful stuff and LOVE the ASP.NET MVC project type .. so i feel like i'm going backwards with some other projects i'm maintaining. I'm using a rewritting rule to bound the RESTFUL url to a method in my ashx file. Input are simple HTTP-GET or POST, using querystring or post params. output json. Clients are anyone in the public interweb space instead of a closed single 3rd party consumer.
Pure.Krome
I haven't done any ASP.NET MVC yet - could you get the rewriting to pass in bits of your URL as method arguments? That would be neat :)
Jon Skeet
A: 

Can i define the response type? eg. application/json?

Yes, see this thread - http://forums.asp.net/p/1054378/2338982.aspx

SUMMARY: You should add a ScriptManager with a ServiceReference inside pointing to your Web service and then invoking your method in the javascript function like this: WebService.HelloWorld(OnSucceeded);

adatapost
hmm. not good for me. I'm not using MsAjax but jQuery .. and i don't want to add any ScriptManager stuff. Especially if this is for an API to be consumed ... any one can use it and shouldn't have to be using MsAjax (unless I've mis-understood your answer).
Pure.Krome
+1  A: 

Sometimes if im wanting to return JSON i just use a .aspx page with:

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

Then using a .NET Json framework (like the 1 here) i create the json i want it to return.

d1k_is