Friday, April 23, 2004
« Another day of coding | Main | My view exactly! »

I'm a big fan of application optimizations (who as a developer isn't?).  There's a cool technique that many an ASP.NET programmer doesn't employ (but should) that can increase the performance of a web application.  The vast majority of pages that a web server will return is HTML-based (e.g. via .aspx or .ascx pages).  However, the time may arise in which you want to return straight XML or some binary data such as a JPEG or GIF, perhaps even a WAV file or something of that nature.

Well, it's pretty easy to write the following in an .aspx page:

private void Page_Load(object sender, EventArgs e) {
   Response.Clear();
   Response.ContentType = “text/xml“;
   // output your xml here
}

This works pretty well.  The client makes a request to the server and gets the xml back.  Simple.

But...every .aspx page is (by default) processed by the PageHandlerFactory class.  This class has a lot of code (and overhead) to handle page requests.  If all you're wanting to do is return XML or an image for example, you can create your own handler for the request.  You do this in one of two ways:  1) implement the IHttpHandler interface or 2) create an .ashx page.

If you implement the IHttpHandler interface you'll have to do a little bit of wireup code in your web.config file to associate a client request with your class, for example:

<add verb=“*” path=“*.ext” type=“NS.MyHandler, MyHandlerAssembly” />

I'm being simplistic here and not going into all of the details, of course, but that's the gist.  Note, you'll also need to go into IIS and map the extension to the aspnet_isapi.dll ISAPI extension.  This is a powerful concept!  The requested file doesn't even need to exist!  IIS (or rather, the ISAPI) will intercept the call to the file (extant or not) and route the request to ASP.NET which in turn takes the request and your IHttpHandler implementation will do the processing.

The second approach, may be a bit more cumpersome on the dev-side but simpler on the admin side.  All you do is create a file with the .ashx extension.  This extension is already mapped in IIS to the aspnet_isapi.dll ISAPI and will automatically be picked up by ASP.NET and processed.

A disadvantage of using the .ashx approach is there is no intellisense/color-coding within VS.NET 2003 and prior (haven't yet tested on Whidbey - but I'll give it a try and report back).  As a point of clarification, there is no intellisense nor color coding when editing your source directly within the .ashx file.  However, if you opt to use code behind, you will have full IDE support.  Also, you add the <% @ WebHandler %>directive to the top of your document.

<%@ WebHandler language="C#" class="classname" %><%@ WebHandler language="C#" class="classname" %>

I found little to no documentation on this handler directive but it has been mentioned in a few MSDN articles and what not.

Regardless of the route you take, all you then need to do is provide implementation for two methods on the IHttpHandler interface:

bool IHttpHandler.IsReusable {
  get { return true; }
}
 
void IHttpHandler.ProcessRequest(HttpContext context) {
}

Then you're off to the races!

This is a powerful technology that was really only previously existed for C++ ISAPI developers and is now at our fingertips.

 

Friday, April 23, 2004 6:17:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [4]  |  Trackback
Saturday, April 23, 2005 10:57:00 AM (Mountain Standard Time, UTC-07:00)


How do I implement this ?



void IHttpHandler.ProcessRequest(HttpContext context) {

}

Steve
Saturday, April 23, 2005 1:01:00 PM (Mountain Standard Time, UTC-07:00)
Steve,



Well, first of all, the main point of the ProcessRequest is to take the incoming request and spit back the appropriate information in the IHttpHandler.ProcessRequest() method. Usually, this is accomplished via the HttpResponse object.



For example, if you wanted you might implement the following to return a very basic XML document:



void IHttpHandler.ProcessRequest(HttpContext context) {

HttpResponse response = context.Response;

response.ContentType = "text/xml";

response.Write("<?xml version="1.0" ?>");

response.Write("<document><content>data</content></document>");

}



Of course, that is rather contrived, but it illustrates the point. You can read data from a database, file, etc or generate it dynamically and return it to the client.



You can open/generate a bitmap and .Save() it to the response stream to return an image to the client...or anything else you need.



Basically, the magic happens via the HttpResponse object. Does that help?
Sunday, January 21, 2007 8:53:00 PM (Mountain Standard Time, UTC-07:00)
- rated a 5.
Hand
anonymous rating
Wednesday, September 12, 2007 10:14:00 AM (Mountain Standard Time, UTC-07:00)
- rated a 5.
Useful
Jim
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview