Tales from the Evil Empire

Bertrand Le Roy's blog

News

Ads Via DevMavens

ASP.NET AJAX UpdatePanel Control: Add Ajax interactivity to your ASP.NET 2.0 web pages


follow bleroy at http://twitter.com

Bertrand Le Roy




Add to Technorati Favorites

Blogs I read

My other stuff

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.

Comments

Jeff Parker said:

Handlers are a great thing, I do wish they were more documented. But you can offload a lot of time comsuming processing to Handlers. I wish I could find the article on MSDN somewhere that shows how to use a threaded timer in a HttpHandler to do the heavy lifting of a website things like email and so on, It was written by one of the guys working on ASP.NET a knowledgable guy by the name of Ron Howard.
He touches on this technique in this article below but he has a complete write up on it somewhere.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp11022004.asp

But you can use handlers for security as well this is one of the things like I said that has great potential unfortunately so very lightly documented http://www.dotnetspider.com/technology/kbpages/1011.aspx
# March 3, 2005 9:18 AM

Bertrand Le Roy said:

I think the guy you're looking for is Rob Howard. Ron Howard is the bald guy who makes movies and used to play Richie Cunningham in Happy Days.
You can get in touch with Rob on his blog:
http://weblogs.asp.net/rhoward/
# March 3, 2005 1:56 PM

Jeff Parker said:

Hah, yes you are correct Rob Howard. My Bad, well we both knew who I meant anyway.
# March 3, 2005 2:16 PM

Salman said:

Yup its Rob Howard. Jeff P., download the latest version of CS and you will see that code snipped.
# March 9, 2005 7:40 AM

TrackBack said:

Bertrand LeRoy illustrates how easy (well, relatively) it is in Whidbey/Visual Studio 2005 to create HttpHandlers (.ashx files). Ease of development aside, he points out that handlers are a comparatively less-understood feature in ASP.NET. He says: "T ...
# March 14, 2005 6:44 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)