Home > Computer Science, Web Programming > Web forms or ASHX (Handlers) (a short differentiation)

Web forms or ASHX (Handlers) (a short differentiation)

“What should I choose [Web-forms or Handlers] when I want a simple response from the server? “. After 6 years of development with C#.net I heard this question a lot. Due to this, I decide to answer it here.

Web forms VS ASHX

Fig 1.0

Assume that you are developing a JQuery AJAX based application that sends its requests to servers and receives a specific response from it. In this case the first solution that may strike you is using handlers. Maybe because of some features I list here:

–          It returns only a specific response; it is so light and is not derived from a heavy class that may have some performance penalties.

–          Unlike a web form it doesn’t need to compile its interface, generates HTML and returns the generated HTML to clients.

–          Unlike web services (mostly used before .Net 3.0), it returns almost all of request’s properties. One of the most prominent one is users’ identity.

Yes, using Handlers has many, many benefits but choosing to use them for such purpose may bring many problems as well.

In a standard web-form programming (I’d better say as a good OOP practice) you should implement a multi-layered

Web forms VS ASHX (Handlers)

Fig 2.0

inheritance for all of your web-forms. It is totally a beginner’s mistake to inherit a web-form directly from System.Web.UI.Page.

In this approach (Fig 2.0) you distribute each group of actions to separate layers. For instance, you place secure parts (handling authorized users) inside SecurityWeb class, non-secure parts placed into NonSecureWeb class etc. Finally, all of your customized classes can inherit from WebBase class which executes your common and shared operations. WebBase class is also derived from the built-in System.Web.UI.Page class.

Now, you have built a clean architecture for security, caching, common operations/properties and many more. Just assume that you want to enforce all of the functionalities you defined in your layered architecture take place in you AJAX handlers. Assuming that you handle the incoming requests with a handler class. A handler class that cannot inherit anything from a parent class. So there is almost no clean and neat way to have all of your efforts happen before executing the handler’s block. You have no choice to make external calls to other classes by passing heavy objects and parsing the return values!!

This is just duplicating many of your codes that results in inconsistent and non-secure consequences.

Better solution:
The alternative is to replace the handler class with a web-form one that inherits from an/other parent class/es. Now you can have all of the important actions happen in almost everywhere without any code duplication or security problem outcomes.

Indeed, Web forms are heavy, very heavy. Here I provided my common practice to decrease some weigh of this fatty class.

How to make web forms act close to handlers:

By adding the following code you can easily change the behavior of ASPX files to act similarly with ASHX files:

Response.Clear();
Response.ContentType = "text/html";
Response.Expires = -1;
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Line6: string response = "";</pre>
<pre>// your process over Request and generate the Response</pre>
<pre>Response.Write(response);
Response.End();

Above, is a sample that returns a text. Obviously, you can change it to any desired return value. Line 6 is where you should place your operation to feed the response variable.

You reached the end:). I should thank for your attention, deeply appreciate your sharing thoughts about this post and looking forward to receiving them here 😉

Advertisements
  1. July 3, 2011 at 5:42 am

    I am curious about what you have discovered thus far.

    My issue is security for my XML data source, should it be ASPX web page or ASHX handler.

    So far, I think as a simple data source, where the it is unnecessary to maintain the ASP.NET\HTML presentation baggage is not necessary, then ASPH is great.

    • July 3, 2011 at 10:25 am

      If you meant ASHX in your last sentence, my answer is no. Because you can remove all HTML section of your response object by simply call Response.Close() so there will be no HTML in you response object. I believe that using Handlers sacrifices some benefits that I described in this article.
      And for security of XML datasource you can implement some encryption over your sent data. If I understood you correctly it is not important that ASPX is being used or ASHX.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: