Roll Your Own HttpHandler
page 4 of 7
by Brendan Enrick
Average Rating: 
Views (Total / Last 10 Days): 34372/ 64

Configuring the Web.config

Before we can see this handler in action, we need to wire up the web configuration file. This file can be found in the root of your web application project or web site. In this file is an HttpHandler section. It contains the information related to these handlers. We need to define ours in here. The important information is the fully-qualified name of our HttpHandler's type. This means the full namespace followed by the class name. In our case this is RollYourOwnHttpHandler.SimpleHandler. We also need to include the name of the assembly where the handler resides. I named it RollYourOwnHttpHandler, which happens to also be the namespace.

The verb we define as * means that we want this handler to take care of all of the different actions for these. This means it will handle everything including gets and posts. We define a path which we want to handle. The * is a wildcard so we want to handle any request whose path ends in .hello; so we have effectively created a fake file extension here for this handler. The type attribute uses the other information we discussed, the fully-qualified name of our HttpHandler type followed by a comma and then the name of the assembly.

Listing 5: Configuration

    <!--Other config information here-->
      <!--Other config information here-->
        <add verb="*" path="*.hello" 
            type="RollYourOwnHttpHandler.SimpleHandler, RollYourOwnHttpHandler"/>
      <!--Other config information here-->
    <!--Other config information here-->

If we use a path of "*.aspx" then our handler will intercept's normal traffic. This gives us some neat functionality. We just need to make sure that at the end of our ProcessRequest method we pass control back over to the standard ASP.NET handler or we will have eliminated our normal ASP.NET traffic.

Configuring IIS Application Extensions (optional)

If you are using a path which is not normally mapped to ASP.NET, you will need to let IIS know that it needs to send the traffic to be handled by ASP.NET. This does not need to be done for the development server built into Visual Studio. So if you are running in Visual Studio, this task is not necessary until you are going to deploy this to a server running IIS.

Launch IIS and right click on the web site you are using. Select Properties. In this window navigate to the tab labeled Home Directory. Click on the Configuration button. This will open up the Application Configuration window. We need to add another application extension, so click on the Add button. The Executable should be the file path of aspnet_isapi.dll, which can be found in your .NET Framework directory. This will vary based on your operating system as well as the version of the .NET framework you are using. The Extension should be the extension you want to map; in our case it is the .hello extension.

View Entire Article

User Comments

Title: Roll Your Own HttpHandler   
Name: namyaf
Date: 2009-07-31 1:47:12 PM
This is the second article that I read by you, and both are explained very well without assuming too much from the reader. Great job and I look forward to more of your articles! peace yo!
Title: Roll Your Own HttpHandler   
Name: Ryan A
Date: 2008-07-16 9:49:57 AM
Good article with quick, easy to follow examples. Thanks for sharing.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-24 12:04:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search