Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding of practice usage of ASP.NET 5 Middleware

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?

like image 651
Friend Avatar asked Apr 17 '15 13:04

Friend


People also ask

What is the use of middleware in asp net?

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.

What is middleware in ASP.NET Core with example?

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.

What is ASP.NET Core middleware and how it is different from HttpModule?

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.


1 Answers

It's actually quite simple. You would create a middleware to get a hold of a request and decide:

  • Whether you want to process it and pass it to next middleware.
  • Whether you want to process it, generate a response and cut the request lifecyle there.

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:

  • ASP.NET 5 Routing Middleware
  • ASP.NET 5 Authentication Middlewares
  • ASP.NET 5 Error Handler Middleware

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.

like image 79
tugberk Avatar answered Oct 10 '22 10:10

tugberk