Top 10 Middleware in ASP.NET Core Web API

Top 10 Middleware in ASP.NET Core Web API

Last Updated on July 13, 2025 by Aram

Middleware is the heartbeat of ASP.NET Core Applications.

To build a clear, scalable, and secure APIs, you have to know about the key collection of middleware that you should use.

So this article will shed the light on the top 10 Middleware in ASP.NET Core Web API that you have to know about.

And knowing the right ordering of the middleware in the ASP.NET Core pipeline is as important as using them.

So I will also be sharing with you the most important rules that you have to follow in order to avoid getting into troubles of debugging unexplained or unexpected behaviors after running your application.

So let’s get started:

The Top 10 Middleware in ASP.NET Core Web API

1. Forwarded Headers Middleware

You’re deploying behind a reverse proxy like NGINX or Azure App Gateway and want the actual client IP.

2. HTTPS Redirection Middleware

Redirects all HTTP requests to HTTPS automatically.

Use this middleware to secure sensitive API traffic, especially login and payment endpoints.

You can force users to access your banking or e-commerce app over secure HTTPS for data protection.

3. CORS Middleware

Cross-Origin Resource Sharing or CORS is responsible to relax the constraints set by the SOP – Same Origin Policy. This is why you have to use CORS wisely and carefuly to not allow unwanted connections to your API from sites that doesn’t have direction integration with your APIs, or even sites that you don’t trust.

So UseCors allow a frontend app on some domain to access your API on another domain.

This is also application for localhost, whenever you are running your API and your app on your same testing machine, you can test the CORS policies that you will define on the different ports that are connecting to your running API/App servers.

4. Routing Middleware

Enables endpoint routing by matching the incoming request to configured routes for the associated controller or Minimal API endpoint.

Since .NET 6, apps don’t require to call UseRouting() manually since this is already handled from the WebApplicationBuilder.

However, UseRouting() can still be called explicitly to override the default behavior, so for instance you can call it after injecting a custom middleware

5. Authentication Middleware

Authenticates users via JWT or cookie tokens.

Allow users to log and access protected resources only if authenticated.

7. Rate Limiter Middleware

Rate limiting is very important measure to throttle the requests and only allow a certain number of requests at a given time window.

This can have multiple advantages from improving the performance, reducing costs of service usage, while reducing the possibility of major attacks on your APIs, like DDoS.

There are 4 types of rate limiting that can be configured through extension methods:

  1. Fixed Window
  2. Sliding Window
  3. Token Bucket
  4. Concurrency

Rate limiting can be either applied globally or via named policies where you can apply it to specific pages or endpoints.

The below applies 1 minute fixed rate limit for only 20 requests with 4 requests in queue.

8. Response Compression Middleware

Used to reduce the response payload size, this middleware compresses HTTP responses using common methods like Gzip, Deflate, or Brotli.

The general guideline here is that you should only compress responses that are not natively compressed.

Image assets like PNG or JPEG are natively compressed response. While HTML, CSS, JS are not natively compressed.

Keep in mind that adding compression to natively compressed responses will not give a good reduction in size while it will add the overhead of decompression, so the compression might cause reduction in performance.

9. Exception Handling Middleware

Catches unhandled exceptions and redirects to an error handling route or returns a formatted response.

In a production API, instead of exposing stack traces to users, redirect them to a friendly error page or return a JSON object with a generic error message.

Starting .NET 8, you can use the IExceptionHandler to easily implement a custom and generic exception handling middleware. Its simplicity comes from the method TryHandleAsync which provides a great abstraction for accessing the http context, the exception object, along with the ability to pass a cancellation token since it supports asynchronous programming.

Use this middleware to return a generic response message while still being able to pair it with Problem Details for an extensive and structured error response.

10. Endpoints Middleware

Executes the endpoint by invoking the matched route handler which is selected by the routing middleware.

In other words, after UseRouting identifies the route, UseEndpoints triggers the controller’s action method.

12 Rules for Middleware Ordering in ASP.NET Core Web API

Below you can find the 12 rules for middleware orderning in ASP.NET Core.

Follow this ordering to stay on the safe side and avoid hours of debugging for unexpected behaviors after running your application.

  1. Use UseForwardedHeaders() first if behind a proxy.
  2. Force redirect to HTTPS early with UseHttpsRedirection().
  3. Call UseRouting() before any middleware that depends on route data.
  4. Apply UseCors() after routing but before authentication, and before response caching.
  5. Add UseAuthentication() before authorization.
  6. Always place UseAuthorization() after routing to enforce policies.
  7. Put UseExceptionHandler() near the top to catch all errors early.
  8. Set UseRateLimiter() early to shield your API from overload.
  9. Call UseResponseCompression() after routing and before endpoints.
  10. Register UseStaticFiles() before routing only if serving static content.
  11. Place custom middleware (e.g., logging, tracing) early to cover the full request.
  12. UseEndpoints() must be last to execute matched endpoints and terminate the pipeline.

Conclusion

Middleware is at the core of ASP.NET Core application, you need to know about the collection of top middleware that will power up your application, in this article we got introduced to the top 10 middleware in ASP.NET Core Web API.

References

Recent Posts

Bonus

Enjoy this brilliant masterpiece of Italian Baroque music:

Arcangelo Corelli: Concerto grosso in D major, Op. 6 No. 1 – Bremer Barockorchester

Leave a Reply