Develop a HttpHandler with full IntelliSense

Ashx files have a bad reputation. There is little documentation about them in v1, and no support for them in Visual Studio 2003. With ASP.NET 2.0 and Visual Studio 2005, this changes, and it becomes as easy to develop an ashx file as any other class.
But what is an ashx file, you may ask? It's an HttpHandler, a class that handles an http request. An ASP.NET page can be considered a kind of elaborate HttpHandler, for example. There are cases where you won't need all the Page infrastructure, WebControls, events and all that. Let's say that you want to stream a thumbnail image to the client, for example. All you need is a reference to the context (to be able to get some information from the QueryString, send data to the response, etc.). That's no more and no less than what the HttpHandler infrastructure gives you.
So when you need raw treatment of a http request, use a handler instead of a page.
I've made a few screen copies while developing a very simple handler, so that you can see how easy it becomes to develop an ashx file in Visual Studio 2005 (click on the images to get them at full resolution):
 
Step 1
Step 1: Adding a new item to the project. I'm choosing "generic handler", which will create the ashx file with the code structure already there.
 
Step 2
Step 2: This is the code structure that Visual Studio provides. I did not write a single character at this point.
 
Step 3
Step 3: I have full IntelliSense and code coloring on my code. Everything works exactly the same as in any other code file.
 
Step 4
Step 4: I also have access to refactoring, immediate squiggly red lines under my syntax errors, etc.
 
Step 5
Step 5: I can build the page, the whole web site or the whole solution (or even just save and let the server auto-compile on the first request).
 
Step 6
Step 6: I can see build errors (I made stupid errors on purpose here, in real life I know how to initialize an array) and click on them to get to the faulty source code.
 
Step 7
Step 7: I'm almost done. The handler compiles.
 
Step 8
Step 8: And it works perfectly!
 
Update: Scott Hanselman has a great HttpHandler template to get you started.

2 Comments

Comments have been disabled for this content.