Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The best thing I like about ASP.NET MVC, is that you can directly call a server method (called action), from the client. This is so convenient, and so straightforward, that I really like to implement such a model in ASP.NET WebForms too.

However, in ASP.NET WebForms, to call a server method from the client, you should either use Page Methods, or Web Services, both of which use SOAP as their communication protocol (though JSON can also be used).

There is also another substitution, which is using Generic Handlers. The problem with them however is that, a separate Generic Handler should be written for each server method. In other words, each Generic Handler works like a simple method.

Is there anyway else to imitate MVC model in ASP.NET WebForms?

Please note that I can't change to MVC platform right now, cause the project at our hand is a big project and we don't have required resources and time to change our platform. What we seek, is a simple MVC model implementation for our AJAX calls. A problem that we have with Web Services, is the known problem of SoapException, and we're not interested in creating custom SoapExctensions.

share|improve this question

2 Answers 2

You probably pant to check out the wcf web API. Preview 6 dropped yesterday, it looks real promising. If you can't get as experimental as that, one can make WCF web services return POX or JSON rather than SOAP.

You can also integrate MVC into traditional web forms apps without too much trouble. Personally, I like mcv handlers better than traditional services. Though the wcf web API is changing my mind fast.

share|improve this answer

My preference has been to use a Generic Handler that has a parameter indicating the name of the action being taken and a separate parameter with a delineated list of arguments to be sent along with it. This allows me to identify my action methods in a library and call them either in case statements or reflectively. While reflection is usually a large performance hit, I find that in this scenario it is not significant enough to cause problems. Below is untested psuedo-code that is sort of a break-down of what I've used previously.

public class MyGenericHandler : IHttpHandler

    public void ProcessRequest(HttpContext context)
    {
        string action = context.Request.QueryString["action"];
        string[] args = context.Request.QueryString["args"].Split("|");

        MyController controller = new MyController();  // whatever your controller class is
        MethodInfo mi = controller.GetType().GetMethod(action);

        if (mi != null)
        {
            context.Response.ContentType = "text/html"
            context.Response.Write(mi.Invoke(controller, args));
        }
    }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.