Middleware in ASP.NET Core is a software component that sits between the web server and the application’s request processing pipeline.It is responsible for handling HTTP requests and responses and can perform a variety of tasks such as authentication, logging, caching, and error handling.Middleware can be added to the request pipeline in the Startup class of an ASP.NET Core application.
The middleware pipeline is a series of components that are executed in a specific order to process incoming requests and outgoing responses.Each middleware component performs a specific task and can either pass the request to the next middleware component in the pipeline or terminate the request processing and generate a response.The order of middleware components is important, as each middleware component can modify the incoming request or outgoing response before passing it on to the next middleware component.ASP.NET Core provides several built-in middleware components and custom middleware components can also be created and added to the pipeline.Middleware provides several benefits for web applications, including modularity and reusability, centralized request processing, improved performance,cross-cutting concerns, and customization.
Middleware can be used to implement various functionalities such as caching, security, and optimization to improve the application’s overall performance and security.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseSession(); app.UseCors(); app.UseResponseCompression(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapRazorPages(); });}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
}
else
app.UseExceptionHandler("/Error");
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseCors();
app.UseResponseCompression();
app.UseEndpoints(endpoints =>
endpoints.MapControllers();
endpoints.MapRazorPages();
});
app.UseStaticFiles();UseStaticFiles: This middleware is used to serve static files (e.g., CSS, JS, images) from the application’s wwwroot folder.
app.UseRouting();UseRouting: This middleware is used to match incoming HTTP requests to their respective endpoints based on the URL path and HTTP verb.
app.UseAuthentication();UseAuthentication: This middleware is used to authenticate users and authorize access to protected resources.
app.UseAuthorization();UseAuthorization: This middleware is used to authorize access to protected resources based on the user’s role or policy.
app.UseSession();UseSession: This middleware is used to enable session state in the application.
app.UseCors();UseCors: This middleware is used to enable cross-origin resource sharing (CORS) for the application.
app.UseResponseCompression();UseResponseCompression: This middleware is used to compress responses to reduce their size and improve performance.
app.UseMyCustomMiddleware();UseMiddleware: This middleware is used to add custom middleware components to the pipeline.
Order of Middleware
app.UseMiddleware<ExceptionHandlingMiddleware>();app.UseMiddleware<StaticFiles>();app.UseMiddleware<RoutingMiddleware>();app.UseMiddleware<AuthenticationMiddleware>();app.UseMiddleware<AuthorizationMiddleware>();app.UseMiddleware<CustomMiddleware>();app.UseMiddleware<EndpointMiddleware>();
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseMiddleware<StaticFiles>();
app.UseMiddleware<RoutingMiddleware>();
app.UseMiddleware<AuthenticationMiddleware>();
app.UseMiddleware<AuthorizationMiddleware>();
app.UseMiddleware<CustomMiddleware>();
app.UseMiddleware<EndpointMiddleware>();
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.
It's a component in .NET Core, which is used to perform some logic(s) while incoming/outgoing of request/response. It is similar to HttpHandlers and HttpModules of classic ASP.NET. Each middleware adds/modify the current response and pass the handler to next middleware(in a pipeline manner). In the Startup class, middleware is implemented and invoked.