Home > Articles > Programming > .NET and Windows Programming

HttpHandlers and HttpModules

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close Window

Chris KinsmanJeffrey P. McManus

Learn more…

Sorry, this author hasn't written any articles.

Sorry, this author doesn't have anything for sale.

Sorry, this author hasn't posted any blogs.

In this sample chapter, Chris Kinsman and Jeffrey McManus discuss the functionality of HttpModules and HttpHandlers, two concepts that are new to ASP.NET.
This sample chapter is excerpted from C# Developer's Guide to ASP.NET, XML and ADO.NET, by Jeffrey P. McManus and Chris Kinsman (Addison-Wesley, 2002).

In ASP.old, you just couldn't do certain things using ASP. If you wanted to create something similar to the output caching in ASP.NET, you were forced to step outside ASP.old and use Internet Server API (ISAPI) filters. If you wanted to create a program that handled all files with a certain extension, you had to step outside ASP. If you wanted to write something that participated in the processing of each and every page, you had to step outside ASP.

One of the goals of ASP.NET was to allow you to do everything you could potentially conceive of related to Web programming directly in the product. It shouldn't limit you. To that end, Microsoft added two new concepts: HttpModules and HttpHandlers. These did not exist inside of ASP.old. To find analogous functionality, you had to step outside into the world of ISAPI programming. HttpModules and HttpHandlers are fairly similar to ISAPI filters, but they implement slightly different functionality.

An Overview of ASP.NET Request Handling

To understand how HttpModules and HttpHandlers fit into the scheme of things, you have to understand the way that ASP.NET handles a request. When a request is received by Internet Information Server (IIS), it looks at the extension to determine which ISAPI filter should handle the request. For any of the supported file extensions, such as .aspx or .asmx, the answer is aspnet_isapi.dll. When ASP.NET fires up, it performs almost the same process again. It looks at the request and compares it to the <httpHandlers> section of the .config file. By default, machine.config maps .aspx files to the PageHandlerFactory and .asmx files to the WebServiceHandlerFactory. This mapping determines the HttpHandler (class) that is responsible for handling the request. With the concept of mapping, you can create a new HttpHandler and map it to a new type of request. In fact, this is exactly what Microsoft did with Trace.axd. You will find that it is a new HttpHandler that is registered in machine.config for any request path that ends in trace.axd.

While processing a request received from IIS, ASP.NET raises several events. They are raised in the following order:

  1. BeginRequest
  2. AuthenticateRequest
  3. AuthorizeRequest
  4. AcquireRequestState
  5. ResolveRequestCache
  6. Page Constructor
  7. PreRequestHandlerExecute
  8. Page.Init
  9. Page.Load
  10. PostRequestHandlerExecute
  11. ReleaseRequestState
  12. UpdateRequestCache
  13. EndRequest
  14. PreSendRequestHeaders
  15. PreSendRequestContent

The items in bold represent several of the page-level events that are raised during the execution of a page. Each of these events can be sunk, providing opportunities to participate in the processing of each page in an application. In Chapter 7, "Security," we looked at handling the AuthenticateRequest and AuthorizeRequest events.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on Yesterday No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network