Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP handler vs HTTP module

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 HTTP handler HTTP modules and middle ware?

HttpModules helps you to attach code specific to a application events. HttpModules are tied to System. web. Middleware are configured in Startup.cs code rather than web.config file (entry point for application) Unlike HttpModules, there is full control of what get's executed and in what order.

What is the HTTP module?

An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the request pipeline and have access to life-cycle events throughout the request. HTTP modules therefore let you examine incoming requests and take action based on the request.

Why do we need HTTP handlers models?

Http handlers are an essential part of ASP.NET - they are what handles the request and generates the response. In webforms it is typical for a page (aspx, or maybe ashx) to interpret a request, but this is itself a type of handler (just mapped by default in the main web. config file).


HttpHandler is where the request train is headed. HttpModule is a station along the way.


The two sentences:

An HttpModule will execute for every request to your application, regardless of extension, and is generally used for things like security, statistics, logging, etc.

An HttpHandler is generally associated with a specific extension, and is used for things like RSS feeds, dynamic image generation or modification, and the like.

A little more explanation if that's not completely clear:

The way I think about them - modules "plug in" to the request pipeline, whereas handlers "handle" a specific file extension. So, if you've got a site with a LoggingModule and a PdfHandler, both will execute for a request to http://example.com/sample.pdf, and the logging module alone will execute for a request to http://example.com/page.aspx.

There's a pretty clear article on the difference on MSDN: HTTP Handlers and HTTP Modules Overview


The prime and common goal of HttpHandler and HttpModule is to inject pre-processing logic before the ASP.NET request reaches the IIS Server.

ASP.NET provides two ways of injecting logic in the request pipeline;

  1. Http Handlers: Http Handler helps us to inject pre-processing logic based on the extension of the file name requested. ASP.NET uses HTTP handlers for implementing a lot of its own functionality.For example, ASP.NET uses handlers for processing .aspx, .asmx and trace.axd files.

example: RSS feeds: To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. So when users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.

There are three steps involved in creating Handler 1. Implement IHttpHandler interface. 2. Register handler in web.config or machine.config file. 3. Map the file extension (*.arshad) to aspnet_isapi.dll in the IIS.

IHttpHandler interface has ProcessRequest method and IsReusable property which needs to be implemented. ProcessRequest: In this method, you write the code that produces the output for the handler. IsResuable: This property tells whether this handler can be reused or not.

You can register the handler in web.config file like this

<httpHandlers>
   <add verb="*" path="*.arshad" type="namespace.classname, assemblyname" />
</httpHandlers>

Note: here we are handling any file name with extension arshad.

  1. Http Modules: HttpModule is an event based processor to inject pre-processing logic before the request reaches the IIS Server. ASP.NET uses HTTP Module to implement lots of its own functionality like authentication and authorization, session management and output caching etc.

ASP.NET engine emits lot of events as the request passess through the request pipeline. Some of those events are AuthenticateRequest, AuthorizeRequest, BeginRequest, EndRequest. By Using HttpModule you can write logic in these events. These logic get executed as the events fire and before the request reaches IIS.

There are two steps involved in creating Modules, 1. Implement IHttpModule interface 2. Register module in web.config or machine.config file

example: Security: Using HTTP module, you can perform custom authentication or other security checks before the request reaches IIS.


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.


HttpHandler is responsible for handling http request by extension while HttpModule is responding to application life cycle events.


Nice article aboute it HttpModule-and-HttpHandlers

Reference: INFO: ASP.NET HTTP Modules and HTTP Handlers Overview

“Modules are called before and after the handler executes. Modules enable developers to intercept, participate in, or modify each individual request. Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET Framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request”.


HTTP handler is where actually compilation is done based on setting. such as if page extension is .aspx then it will compile through system.web.Ui.Pagahandlefactory. once compilation is done at HTTP handle request will go though HTTP module and IIS.