In .NET core when we make any HTTP request to server it goes with multiple building blocks of code called Middleware.
The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the other.
Middleware in .NET Core is a component that is executed in the request pipeline to handle HTTP requests and responses. It plays a crucial role in processing requests before they reach the application’s core logic and modifying responses before sending them back to the client. Middleware components can perform operations such as authentication, logging, exception handling, request transformation, and response modification.
Example:
Suppose we want to authenticate HTTP request before reaching to Server then we can add code logic to authenticate in request pipeline then we can call that logic as Middleware.
Another example:
Suppose we want to Add some header to each response for request and code logic that will perform to add headers to response is called middleware.
How Middleware Works in .NET Core
Middleware follows a pipeline pattern where multiple middleware components execute sequentially. Each middleware can:
1. Process the request and decide whether to pass it to the next middleware.
2. Process the response before returning it to the client.
Middleware Execution Flow
1. A request comes in.
2. The first middleware processes it and passes it to the next middleware.
3. Each middleware in the pipeline processes the request.
4. The last middleware in the chain processes the request and generates a response.
5. The response moves back through the middleware pipeline, where each middleware can modify it before sending it to the client.
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.Run();
app.Use(async (context, next) => { Console.WriteLine("Middleware 1: Before Request"); await next(); Console.WriteLine("Middleware 1: After Response"); }); app.Use(async (context, next) => { Console.WriteLine("Middleware 2: Before Request"); await next(); Console.WriteLine("Middleware 2: After Response"); }); app.Run(async context => { Console.WriteLine("Middleware 3: Request Processed"); await context.Response.WriteAsync("Final Response"); });
Middleware 1: Before Request Middleware 2: Before Request Middleware 3: Request Processed Middleware 2: After Response Middleware 1: After Response
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.Use(async (context, next) => { Console.WriteLine("Middleware 1: Before Next()"); await next(); // Passes control to the next middleware Console.WriteLine("Middleware 1: After Next()"); }); app.Use(async (context, next) => { Console.WriteLine("Middleware 2: Before Next()"); await next(); Console.WriteLine("Middleware 2: After Next()"); }); app.Run(async context => { Console.WriteLine("Middleware 3: Handling Request"); await context.Response.WriteAsync("Final Response"); }); app.Run();
Middleware 1: Before Next() Middleware 2: Before Next() Middleware 3: Handling Request Middleware 2: After Next() Middleware 1: After Next()
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.Run(async context => { await context.Response.WriteAsync("Hello from `app.Run()`!"); }); app.Run();
Copyrights © 2024 letsupdateskills All rights reserved