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.