tags:

views:

759

answers:

2

Hello, What is the best practice to support data for asp.net 2.0-3.5 ajax web application? I don't want to use update panels, just plain text data (JSON). Should I use web services? Or is there another way.

+1  A: 

You can use plain aspx pages or handlers and just output JSON. You do this by erasing all the Html in the aspx and then using Response.Write() in the code.

Then for the front end JS you can use JQuery or any other Ajax framework.

You may also want to check out Asp.Net MVC. MVC has a JsonResult resonse type and is very easy to use together with JQuery to get very good Ajax functionality.

Sruly
I tried MVC and it's really great. But i'm stuck with some legacy asp.net code in work. No chances for .net 3.0 - so MVC is not an option.
ppiotrowicz
+4  A: 

Errrr... Use an .aspx page ? What are handlers for ?

You just have to create a generic base handler that will take care of json (de)serialization (e.g. using Json.net) and then implement handlers for your ajax calls.

public abstract class JsonHandlerBase<TInput, TOutput> : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        TInput input = (TInput)context.Request; // Desesialize input
        TOutput output = ProcessRequest(context, parameter);

        string json = (string)output; // Serialize output
        context.Response.Write(json);
    }

    public abstract TOutput ProcessRequest(HttpContext context, TInput input);

    public bool IsReusable { get { return false; } }
}

This is just an example, it's up to you to decide want you need in your base handler.

ybo
This is the better way to do it.
Sruly
Sweet! Thanks :)
ppiotrowicz