4

Can anyone explain me how ASP.NET handles the convertion from a class object to the JSON object in WebMethods?

For example, you have following WebMethod which returns a Person object:

    [WebMethod]
    public static Person GetPerson()
    {
        Person p = new Person() 
        {
            Id = 1,
            Name = "Test"
        };

        return p;
    }

In my jQuery where i call the WebMethod I get a response which contains out of a json object.

How did ASP.NET do this automatcally? Does it uses the JavaScriptSerializer class?

Also you see a lot of examples of using JSON converters to convert your class object to a json object. Why is this? Is it because of the JavaScriptSerializer class it uses and its bad performance or... ?

4
  • 1
    Side note: WebMethod isn't really supported anymore. If you need a JSON or XML based API, it's best to look to ASP.NET Web API, which uses Newtonsoft's Json.NET to handle JSON serialization. Commented Dec 5, 2014 at 13:35
  • I am using ASP.NET Webforms where I have some ajax calls, can I use the Web API inside my webforms project? Commented Dec 5, 2014 at 13:39
  • Yes! You certainly can. Microsoft has this initiative called One ASP.NET. Basically all services should receive the same features (where applicable) and you can mix and match your projects. I actually started out using Web API with Web Forms, though now I'm learning MVC. It's basically as simple as adding a NuGet package, configuring some routing, then you can practically copy your Web Method code into the Web API function. See the link in my first comment for getting started steps. Commented Dec 5, 2014 at 13:42
  • Thanks! I will look into that but I hope to get an answer on this also to fully understand the steps between request and response using ajax calls. Commented Dec 5, 2014 at 13:45

1 Answer 1

3

How did ASP.NET do this automatically?

Basically there's some code sitting between the web and the WebMethod that takes the request, figures out what it's requesting, finds your WebMethod and obtains the result, then serializes it back to the client based on the acceptable formats in the request header.

Does it uses the JavaScriptSerializer class?

Probably. I couldn't find anything out there that stated it. But it doesn't use any third party library. Since that's one is built in, that's a good assumption.

Also you see a lot of examples of using JSON converters to convert your class object to a json object. Why is this? Is it because of the JavaScriptSerializer class it uses and its bad performance or... ?

WebMethod technique can be very finicky and sometimes will refuse to return JSON, despite the accept headers. One way around that is to do something like this:

[WebMethod]
public static void GetPerson()
{
    Person p = new Person() 
    {
        Id = 1,
        Name = "Test"
    };
    HttpContext.Current.Response.ResponseType = "application/json";
    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(p));
    HttpContext.Current.Response.End();
}

You lose content negotiation (unless you manually implement it by inspecting the request headers), but you get more control over how it's serialized.

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

4 Comments

You say WebMethod technique can be very finicky and sometimes will refuse to return JSON. Does this include other convertion libraries like JSON.NET?
When I said refuse to return JSON, I mean it ignores the request headers and serializes as XML instead. That has nothing to do with which JSON serializer you're using, it has everything to do with how WebMethod implementation is coded behind the scenes.
So to make it short, creating WebMethods without a return type and writing your result back on the responsestream using a converter would be the best practice. Or using the ASP.NET Web API?
Use ASP.NET Web API would be the best practice for REST based API. John Saunders wrote 3 years ago about not using Web Method in favor of other technologies. ASP.NET Web API wasn't quite around at that time, but it gives you a good overview of some reasons not to use WebMethod/ScriptMethod/ASMX technology.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.