Tuesday, June 28, 2011

HTTP Handlers and HTTP Modules

HTTP Handler
- refered as endpoint
- runs in response to request made in application
- modules that handle the response
- common page handler = *.aspx, web service handler = *.asmx
- typical uses : RSS feeds, Image server

HTTP Modules
- called on every request made by application
- function handle the content
- examine incoming and outgoing request + take action based on request
- typical uses : security, statistic & logging, custom header & footer
- also uses to implement various application features, which includes forms authentication, caching, session state, and client script services.

Starting point :
- HTTPHandler (synchronous handler)
- Not return until HTTP request called process finished.
- HTTPModule
- Asynchronous handlers
- runs process independently sending response to user.
- must implement the BeginProcessRequest & EndProcessRequest method

Source code puts in App_Code folder, when compiled put in Bin folder
Developed using IIS 6.0, can be used in IIS 7.0 (with a little/no change)

Creating HTTP modules
1) implements IHttpModule interface
2) write handler for Init method
* append something to response, uses EndRequest event
* perform custom authentication logic, uses AuthenticationRequest event
3) Write the code that you used.
4) Implement Dispose method for cleanup (optional)
5) Register module in Web.config
Creating HTTP handler
creating a class that implements the interface.