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?
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.
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
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