Raj Bhatt
What is Middleware in ASP.NET Core ?

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.

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. if (env.IsDevelopment())
  4. {
  5. app.UseDeveloperExceptionPage();
  6. }
  7. else
  8. {
  9. app.UseExceptionHandler("/Error");
  10. }
  11. app.UseStaticFiles();
  12. app.UseRouting();
  13. app.UseAuthentication();
  14. app.UseAuthorization();
  15. app.UseSession();
  16. app.UseCors();
  17. app.UseResponseCompression();
  18. app.UseEndpoints(endpoints =>
  19. {
  20. endpoints.MapControllers();
  21. endpoints.MapRazorPages();
  22. });
  23. }

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

  1. app.UseMiddleware<ExceptionHandlingMiddleware>();
  2. app.UseMiddleware<StaticFiles>();
  3. app.UseMiddleware<RoutingMiddleware>();
  4. app.UseMiddleware<AuthenticationMiddleware>();
  5. app.UseMiddleware<AuthorizationMiddleware>();
  6. app.UseMiddleware<CustomMiddleware>();
  7. app.UseMiddleware<EndpointMiddleware>();
By Raj Bhatt in .NET Core on Mar 18 2023
  • Sandeep Kumar
    Jul, 2023 12

    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.

    • 0
  • Snehlata Shaw
    Apr, 2023 12

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS