Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 183 Comments | 497 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

After endless searching for answers to my handler setup questions, I finally decided to write an all-inclusive set of articles about how someone should go about this. If you find any errors or see room for improvement in any way, please let me know - I will make sure to note any contributions in the "Special Thanks" section. This will be my first contribution to this article-set.

Introduction

All requests to IIS are handled through Internet Server Application Programming Interface (ISAPI) extensions. ASP.NET has its own filter to ensure pages are processed appropriately. By default, the ASP.NET ISAPI filter (aspnet_isapi.dll) only handles ASPX, ASMX, and all other non-display file formats used by .NET and Visual Studio. However, this filter can be registered with other extensions in order to handle requests to those file types, too, but that will be covered later.

Every request flows through a number of HTTP modules, which cover various areas of the application (i.e. authentication and session intofmation). After passing through each module, the request is assigned to a single HTTP handler, which determines how the system will respond to the request. Upon completion of the request handler, the response flows back through the HTTP modules to the user.

HTTP Module

HTTP modules are executed before and after the handler and provide a method for interacting with the request. Custom modules must implement the System.Web.IHttpModule interface. Modules are typically synchronized with events of the System.Web.IHttpModule class (implemented within the Global.asax.cs or .vb file). The following consists of a list of events that should be considered when implementing your module:

  • BeginRequest
  • AuthenticateRequest
  • AuthorizeRequest
  • ResolveRequestCache
  • AcquireRequestState
  • PreRequestHandlerExecute
  • PostRequestHandlerExecute
  • ReleaseRequestState
  • UpdateRequestCache
  • EndRequest
  • PreSendRequestHeaders*
  • PreSendRequestContent*
  • Error*

The events identified by an asterisk (*) can occur at any time within the request; all others are listed in their calling order.

HTTP Handlers

HTTP handlers proces the request and are generally responsible for initiating necessary business logic tied to the request. Custom handlers must implement the System.Web.IHttpHandler interface. Additionally, a handler factory can be created which will analyze a request to determine what HTTP handler is appropriate. Custom handler factories implement the System.Web.IHttpHandlerFactory interface.

More on Handlers...

When to use Modules and Handlers

With all of the options available in ASP.NET, it is sometimes hard to determine what solution best fits your needs. Of course, it's always best to keep things simple; but, you still need to take evolutionary considerations and experience levels of current and future team members who have a potential of working on teh project into account. Both modules and handlers add a layer of indirection that can be daunting to beginners and/or programmers who are not used to implementing quality designs (read: design patterns).

First, consider what it is that you want to do within your module or handler. Some functions, such as authentication and intstrumentation can be added within modules. Modules should be considered only when there is a need for pass-through and/or intercepting interaction with the request. Alternatively, handlers should be put in place when there is a need to direct functional actions to one or more parts of an application. Probably the most noted use of HTTP handlers is to the FrontController pattern, which allows requests to be refactored and assigned to different components within your application without implementing such changes in every page.

Second, is it worth it? Most applications do not require this level of indirection, which can be hard to understand and/or implement when not documented properly. Some methods, such as the PageController pattern, allow for common functionality to be reused across multiple pages by including this logic in a base System.Web.UI.Page object, and inheriting from this for every web page. When reviewing the PageController implementation, you should know and understand the appropriate use of inheritence. Although certain things can be done this way (i.e. authorization and instrumentation), this is not always the correct means. You should fully understand the pros/cons of utilizing both modules and handlers before deciding on one implementation over the other.

With each of these considerations, and more, the decision to implement modules and/or handlers can be a daunting one. Such decisions should be led by an experienced .NET architect. In the absense of a skilled architect, you will be looking at a lot of leg-work to determine the best solution.

Conclusion

HTTP modules and handlers can be complex. Take the time to fully understand their pros/cons before implementing a solution. I recommend exploiting the experience of software architects whether in your organization or in the community. Whatever you choose, good luck on your ventures. I am finalizing my HTTP handler project, so I should be releasing an article with a sample implementation as well as any recommendations I may have for approaching the job within the next few weeks.

References

Special Thanks

TBA

History

1.0   Genesis

posted on Sunday, May 23, 2004 7:25 PM

Feedback

# re: Introduction to ASP.NET HTTP Modules and Handlers 8/31/2004 12:06 AM santhosh nag
really not convincing, i think you have to explain more on module and handler, with some traceble example.

# re: Introduction to ASP.NET HTTP Modules and Handlers 1/27/2005 10:55 AM Michael Flanakin
This isn't meant to be a "convincing" article. This is simply meant to give you a broad overview of what HTTP modules and handlers are for.

# re: Introduction to ASP.NET HTTP Modules and Handlers 2/7/2005 5:57 AM Ravi Perera
Appreciate if you could help me with the following. I wants to display the requested file (if it is a valid request- validated using the ValidUserRequest() method). How do I do that.

For example user request for http://www.mysite.com/myfile.txt and if the user logged into the site I wants him to see the file else display a message. How do I achive this?

Appreciate if you could email me on ravi.perera@emirates.com

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Try
If ValidUserRequest() = False Then
context.Response.Write("Invalid request")
Else
'If valid request, then I wanted to display the
'requested file. How do I do that?
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

# re: Introduction to ASP.NET HTTP Modules and Handlers 2/7/2005 4:49 PM Michael Flanakin
Ravi,
I suggest you review my HttpHandler article (http://geekswithblogs.net/flanakin/articles/HttpHandlers.aspx). After reviewing this, you'll need to change your IIS settings to allow the ASP.NET ISAPI filter (aspnet_isapi.dll) to accept all requests (*). Then, in your handler, if the user is validated, simply change the Response.ContentType and use Response.WriteFile() to output the file. This is all you need to do. After that, you can simply Response.End() to cancel any other processing. I would suggest handling an invalid request in a better way, but that's up to you.

Hope this helps!

# re: Introduction to ASP.NET HTTP Modules and Handlers 7/26/2005 5:41 PM stephane viau
how to add custom http headers in the client request using httpmodule. the headers collection of request object is read only.
i need to intercept a request, and add a couple of headers that are required for an sso solution'

thanks

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/7/2005 8:16 AM Mark
I'm having the same problem. So far as I can tell it is not possible to inject headers into a request using an HttpModule. I think that you would have to write an ISAPI filter or extension to do this.

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/7/2005 5:39 PM Michael Flanakin
If I understand you correctly, you cannot add a handler from within a module. What you can do is add both and set some property in the context to determine whether or not to process the handler. To do that, you can use the Context.Items collection, I believe. I may be wrong on this, tho. I can't check it right now. If someone else has the chance to check, please post your results. Hope this helps, and thanks to anyone who has the chance to look into it!

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/20/2005 9:17 AM pt_linx
Anyone able to obtain a handle to session and access variables when creating a custom HttpModule?
I know that session is supposed to be available during the AcquireRequestState call however it is always null. I have searched high and low for about 12 hours now and have not found any solutions - any help with this issue will be much appreciated.

# re: Introduction to ASP.NET HTTP Modules and Handlers 9/18/2006 11:17 PM pragadesh
sir iam not satisfied with this article. You have to explain more in detail with examples.

# re: Introduction to ASP.NET HTTP Modules and Handlers 9/19/2006 4:27 AM anitha
i need the difference between http modules and http handlers

# re: Introduction to ASP.NET HTTP Modules and Handlers 12/14/2006 3:25 AM kesavan
I am not satisfied with this article. Could you please explain more about this top with examples.?




# re: Introduction to ASP.NET HTTP Modules and Handlers 3/26/2008 4:04 AM Bidisha
sir, I am not satisfied with the content.It should be more specific and hold some examples.

# re: Introduction to ASP.NET HTTP Modules and Handlers 4/21/2008 3:02 PM Sean G.
Here's my take...

if you just want to do something TO a page as it is processsing, you want a module. This would include transaction logging, authentication checking, inserting a custom footer, etc.... The modules run to examine and/or change the page as it is processing. This can be done before or after the handler processes. So, if you want to check authentication, do it in a module event before the handler. If you want to add a footer, use a module event after the handler has processed. The other important thing to realize is you want multiple modules to run -- you want the authentication module to run AND the footer module to run. This is why you need to use a module for any kind of "pipeline" processing.

A handler is simple -- you configure the IIS application (web site or virtual directory) to send requests with certain extension to the ASP.NET handler. Then, in web.config you tell ASP.NET to use your handler application. Your application will be passed the IIS Context and you can make it do whatever you want. Just keep in mind that only one handler will run for a request.

Also, keep in mind that if you are building a complete site on ASP.NET, you can find ways to implement common functionality across pages (authentication, footers) with-in your application. Handlers and Modules add more complexity and administration.

# re: Introduction to ASP.NET HTTP Modules and Handlers 10/4/2008 8:28 AM ramlabeevi
Please explain it with examples

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/4/2008 6:51 AM Manisha
Still not understood, why to use them

# re: Introduction to ASP.NET HTTP Modules and Handlers 7/18/2009 9:38 AM tabitha smith
Whats the deal with me not being able to get on youtube through my phone

# re: Introduction to ASP.NET HTTP Modules and Handlers 10/20/2009 4:09 PM Affordable web design
Great guidelines. Thanks.

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/15/2009 10:35 AM aamir Hasan
HTTP Modules and Handlers
fnction BeginRequest
context.Response.Write("Welcome")

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/22/2009 6:14 AM rajneesh
HTTP HANDLER RUN

# re: Introduction to ASP.NET HTTP Modules and Handlers 11/22/2009 6:21 AM RAJNEESH
BROWSER(send request)
-->
(request on server)(run inetinfo.exe)
-->
aspnet_isapi.dll)(internet server application progrmming interface)
-->

WORKER PROCESS
-- APPDOMAIN (multiple application can run in
same process without influence with each
other)
--HTTPModular (session Module,authenticate
module)
-- HTTPHandler(run pagelife cycle and return
result)


Rajneesh Hajela


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: