The ASP.NET Core request pipeline processing works by running middleware components as per the sequence they are placed in the Configure method of the Startup class.
The pictorial representation of request processing describes the execution order of middleware. The sequence order is created by placement of the Use, Run, Map, and MapWhen extension method code placements. Invoking of next middleware decides the order of the middleware execution.
The following are some of the real-world scenarios where middleware order plays an important role:
- Authentication middleware should handle requests initially. Only authenticated requests are allowed to access application.
- In case of web API projects, CORS middleware fits well as initial middleware and then authentication middleware.
- Custom middleware can perform some custom processing before accessing resources, even...