Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extending ASP.NET with HttpHandlers and HttpModules  : Page 4

Write custom file handlers and filters with ease in ASP.NET that perform special request-handling, or alter requests either before or after they're processed by the ASP.NET engine.


advertisement
HttpModules intercept each request to Web application resources. The class that is to act as HttpModule must implement an interface called IHttpModule. Again, the interface is simple:

[Visual Basic] Sub Init(ByVal context As HttpApplication) Sub Dispose() [C#] void Init(HttpApplication context); void Dispose();

To implement the IHttpModule interface, you need only write two methods: Init and Dispose. The Init method receives an HttpApplication parameter. You can alter the request either before or after the handler for that request executes by trapping the HttpApplication BeginRequest and EndRequest events. You write event handlers for these events in the Init method. The Dispose method lets you clean up any unmanaged resources.

To get started, just as with the HttpHandler example, create an empty project and add a class to it. Add a reference to the System.Web assembly and add the Imports statement in the new class. To implement IHttpHandler interface, use the AddHandler statement and write methods to handle the HttpApplication's BeginRequest and EndRequest events, for example:

Imports System.Web Public Class TweakRequestModule Implements IHttpModule Public Sub Init(ByVal app As HttpApplication) _ Implements IHttpModule.Init AddHandler app.BeginRequest, _ AddressOf MyBeginRequest AddHandler app.EndRequest, _ AddressOf MyEndRequest End Sub Public Sub Dispose() Implements IHttpModule.Dispose ' add clean-up code here if required End Sub Public Sub MyBeginRequest(ByVal s As Object, _ ByVal e As EventArgs) Dim app As HttpApplication app = CType(s, HttpApplication) app.Response.Write _ ("<h4>Request Begins Here...</h4>") End Sub Public Sub MyEndRequest(ByVal s As Object, _ ByVal e As EventArgs) Dim app As HttpApplication app = CType(s, HttpApplication) app.Response.Write _ ("<h4>Request Ends Here...</h4>") End Sub End Class

The code attaches event handlers to the HttpApplication instance using the AddHandler statement. The event-handlers themselves (MyBeginRequest and MyEndRequest) perform the actual work to write HTML messages.

Configuring an HttpModule in Web.config
You must modify the configuration settings in web.config to let ASP.NET know about the HttpModule. This time, you don't need to create a new Web project to test the code; instead, add a new Web form in the Web application you created for the HttpHandler sample project and then modify the web.config file. The following fragment shows the <httpModules> section.



<httpModules> <add type="MyHttpModule.TweakRequestModule,MyHttpModule" name="MyHttpModule" /> </httpModules>

As before, you need to specify the type and assembly details of the module. After saving the configuration changes, run the Web Form. You'll see the output in Figure 4.



Bipin Joshi is a Microsoft MVP, software developer and author from Mumbai, India. With a strong background in VB, ASP, and COM, he now works on .NET technologies. He coauthored "Professional ADO.NET" and "Professional XML for .NET Developers" for Wrox Press. You can reach him at his Web site: www.DotNetBips.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date