Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP modules and HTTP handlers in ASP.Net MVC?

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?

like image 909
rohit singh Avatar asked Nov 04 '16 13:11

rohit singh


People also ask

What is HTTP handler and HTTP module in MVC?

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.

What is the difference between HTTP modules and HTTP handlers?

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.

What are handlers in MVC?

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.

What is HTTP handlers in ASP.NET c#?

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.


1 Answers

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.

  • If you want your functionality to only be executed once per Http Request, you should use an HttpModule.
  • ActionFilters may be executed several times in a single trip to the server.

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.
    • For example: if you want to change how 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>
like image 63
Ujain Avatar answered Oct 03 '22 12:10

Ujain