This great article explains things about ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
But it is still unclear about when and why we have to use ASP.NET 5 Middleware.
Can anyone explain it and provide real life examples of its usage?
Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.
Middleware is a piece of code in an application pipeline used to handle requests and responses. For example, we may have a middleware component to authenticate a user, another piece of middleware to handle errors, and another middleware to serve static files such as JavaScript files, CSS files, images, etc.
Both Middleware and HttpModule are pieces of code blocks which lets two or more components interact together during a request execution. HttpModules are registered in the web. config file of the ASP.NET framework while a Middleware is registered via code inside an ASP.NET Core application.
It's actually quite simple. You would create a middleware to get a hold of a request and decide:
You can also have a middleware to act on the responses only (e.g. a middleware who does the compression).
Most of the middlewares are to provide cross cutting functionality such as routing, authentication, compression, error handling. A few real world examples on these:
One confusion could be around framworks here such as MVC, SignalR, etc.. Previously in Katana world, each framework was creating each own middleware to hook into the system. With ASP.NET 5, this is a little different. In order for a framework to process requests, you can hook into the routing middleware by providing a special handler. For example, here is the MVC 6 route handler. When you look at the biuilder extensions, you will see that MVC actually uses the routing middleware.
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