Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MiddleWare vs HttpModules

Starting with Asp.Net 5 we now have the "middleware" concept to change or interact with the request and response. All of this was done before using HttpModules.

So here my question.

What is exactly an asp.net middleware and what issues are addressed by this component that can not be addressed with HttpModules?

like image 222
Juan M. Elosegui Avatar asked Aug 08 '15 17:08

Juan M. Elosegui


1 Answers

HttpModules act as request filters in ASP.NET versions prior to 5. They are reusable units of code that can be plugged into the request pipeline, and tasked with responding to events defined in the HttpApplication class as they are fired.

Middleware can be thought of as both HTTP modules and handlers that we've had in classic ASP.NET. Some middleware would implement various intermediate tasks when processing requests such as authentication, session state retrieval and persistence, logging and so on. Some of them would be the ultimate request handlers that would produce responses.


Ques 2 : what issues are addressed by this component that can not be addressed with HttpModules?

  • In an HttpModule, you had to execute code based on the various stages of a request, things such as AuthenticateRequest, AuthorizeRequest, PostResolveRequestCache, and other slightly confusingly named events. This is no longer the case with Middleware, things just make sense now. everything is simply a linear execution of your code. You can have multiple middleware’s defined to execute in your application and each one is registered explicitly in your Startup.cs file.

  • As a developer, you are in full control of what get’s executed and in what order instead of not knowing which HttpModules are executing and not really in control of their order of execution. Middleware can simply modify a request and continue calling the next one in the chain, or it can just terminate the pipeline and return a result.

Reference

like image 171
Tushar Avatar answered Oct 19 '22 20:10

Tushar