I was asked a question in interview that how to implement HTTP module
and HTTP handler
in ASP.Net MVC
. I know that they are used in ASP.Net to write pre-processing logic before the aspx page is called. But in ASP.Net MVC we have filters for that so i told them we use Filters for that. Did i gave the right answer?
To explain HTTP Modules and HTTP Handlers, HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain. HTTP Handlers are extension based pre-processor whereas HTTP Module are event based preprocessor.
HTTP handler is the process that runs in response to a request made to an ASP.NET Web application. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
MvcHandler. This handler is responsible for initiating the ASP.NET pipeline for an MVC application. It receives a Controller instance from the MVC controller factory; this controller handles further processing of the request.
An ASP.NET HTTP handler is the process that runs in response to a request that is made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes . aspx files. When users request an . aspx file, the request is processed by the page handler.
Action Filters allow you to hook into MVC specific events only, whereas HTTP Modules allow you to hook into ASP.Net events. So even in MVC, to implement a HTTP Module and HTTP handler, you will need to implement corresponding interface.
To explain HTTP Modules and HTTP Handlers, HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain.
jpg
files are processed, you will implement custom HTTP handler versus if you want to execute additional logic during processing of the request, you will implement a custom HTTP module. There is always only one HTTP handler for a specific request but there can be multiple HTTP modules.To Implement an HTTP Handler:
You implement IHttpHandler
class and implement ProcessRequest
method and IsResuable
property. IsResuable
property determines whether handler can be reused or not.
public class MyJpgHandler: IHttpHandler
{
public bool IsReusable => false;
public void ProcessRequest(HttpContext context)
{
// Do something
}
}
Next we need to specify which kind of request will be handled by our custom handler in web.config
file:
<httpHandlers>
<add verb="*" path="*.jpg" type="MyJpgHandler"/>
</httpHandlers>
To implement an HTTP module:
We need to implement IHttpModule
and register the required events in Init
method. As a simple example, if we wanted to log all requests:
public class MyHttpModule: IHttpModule
{
public MyHttpModule() {}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(this.context_BeginRequest);
application.EndRequest += new EventHandler(this.context_EndRequest);
}
public void context_BeginRequest(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(@ "C:\log.txt", true);
sw.WriteLine("Request began at " + DateTime.Now.ToString());
sw.Close();
}
public void context_EndRequest(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(@ "C:\log.txt", true);
sw.WriteLine("Request Ended at " + DateTime.Now.ToString());
sw.Close();
}
public void Dispose() {}
}
And register our module in web.config
file:
<httpModules>
<add name="MyHttpModule " type="MyHttpModule " />
</httpModules>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With