This is the HttpHandler and HttpModule in real scenarios article series. In this series we are learning various concepts and uses of HttpHandler and HttpModule. In our previous articles we covered various important concepts, you can read them here.
- HttpHandler and HttpModule in Real Scenario: Getting Started with HttpHandler
- HttpHandler and HttpModule in Real Scenario: Few examples of HttpHandler-1
- Working with Image in HttpHandler
- Working With Header in HttpHandler
- HttpModule in real scenario: Implement simple HttpModule
- HttpModule in Real Scenario: Multiple HttpModule and Communication
- HttpModule in Real Scenario: Events in HttpModules
- Pass Data from HttpModule to HttpHandler in ASP.NET Application
In this article we will understand a few more concepts and uses of HttpModule. Those concepts and the situation is mapped with a practical problem and solution. So, let’s start with our first example.
Handle exception in HttpModule
Exception handling is a very common task in any application. We can handle an exception using the HttpModule.HttpContext has Error event and it executes when some exception occurs in the application. In this example we will handle an exception with the Error event of the HttpContext class. This is the implementation.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- context.Error += new EventHandler(on_error);
- }
- public void on_error(object sender, EventArgs e)
- {
- //Log Operation goes here.
- HttpContext ctx = HttpContext.Current;
- HttpResponse response = ctx.Response;
- HttpRequest request = ctx.Request;
- Exception exception = ctx.Server.GetLastError();
- string errorInfo = "<p/>URL: " + ctx.Request.Url.ToString();
- errorInfo += "<p/>Stacktrace:---<br/>" +
- exception.InnerException.StackTrace.ToString();
- errorInfo += "<p/>Error Message:<br/>" +
- exception.InnerException.Message;
- response.Write("<p/>ErrorInfo:<p/>");
- response.Write(errorInfo);
- ctx.Server.ClearError();
- }
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- throw new Exception("My Exception");
- }
- <system.webServer>
- <modules>
- <add name="mymodule1" type="WebApp.MyHttpModule1"/>
- </modules>
- </system.webServer>

URL re-writing/redirection operation
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- context.BeginRequest += new EventHandler(begin_request);
- }
- public void begin_request(object sender, EventArgs e)
- {
- HttpContext Context = ((HttpApplication)sender).Context;
- string url = Context.Request.Url.AbsolutePath;
- if (url.Contains("Home"))
- {
- url = url.Replace("Home", "NewHome");
- }
- Context.Server.Transfer(url);
- }
- }
- }
In the following example we see that the URL has changed to “NewHome/WebFomr1.aspx”

This is one cool use of HttpModule.
Prevent any request type in the application
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- context.BeginRequest += new EventHandler(begin_request);
- }
- public void begin_request(object sender, EventArgs e)
- {
- HttpContext Context = ((HttpApplication)sender).Context;
- if (Context.Request.RequestType == "GET")
- {
- Context.Response.Write("Get Request is not allowed in this application");
- }
- }
- }
- }

Conclusion
In this article, we learned a few pragmatic situations for implementing HttpModule as a solution. I hope those examples will help us to understand HttpModule better.
Next Article : Few Important Uses of HttpModule: Part 2

Join the conversation! Your thoughts help the community grow.