Middleware in ASP.NET Core
Middleware in ASP.NET Core
Middleware in ASP.NET Core refers to software components that are assembled into an
application pipeline to handle requests and responses. Each piece of middleware processes an
HTTP request as it flows through the pipeline and can either:
Vidit Tyagi
Middleware Execution Order
Middleware executes in the order they are registered. The request flows down the pipeline, and the
response flows back up.
Key Concepts:
1. RequestDelegate
2. HttpContext
The HttpContext object contains information about the current HTTP request and response,
including headers, body, query parameters, user information, etc. Middleware components
interact with this context to manipulate the flow.
Vidit Tyagi
Step-by-Step Guide to Creating Custom Middleware:
First, you need to create a middleware class that will contain your custom logic. This class
should have an Invoke or InvokeAsync method.
Vidit Tyagi
2. Register Middleware in Program.cs
Vidit Tyagi
Some Examples/ Scenarios where Middleware can be use:
1. Custom Header Middleware
Vidit Tyagi
Conclusion
1. The concept of middleware and how it fits into the request pipeline.
2. Common built-in middleware components like routing, static files, and authentication.
Vidit Tyagi