4

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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?
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.
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 :)
2

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.

1 Comment

+1 for simple and easy to get working with jqueryUI's autocomplete
0

You can simply use

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

This worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.