This is a new feature in .NET 8 that allows you to bypass the middleware pipeline for certain endpoints. This can be useful for improving performance, especially for endpoints that are simple and do not need any authorization or other processing.
To use Route Short Circuit middleware, you simply need to call the ShortCircuit() method on the endpoint.
Working examples:
app.MapGet("/short-circuit", () => "Short circuiting!")
.ShortCircuit();
services.AddRouteShortCircuit(options =>
{
options.Paths.Add("/favicon.ico");
options.OnRejected(context => { /* metrics */ });
});
...
app.UseRouteShortCircuit();
Use Cases:
When a request comes in for the /short-circuit endpoint, ASP.NET Core will immediately execute the endpoint logic and then end the request. This means that the middleware pipeline will not be run for this endpoint.
Route Short Circuit middleware can be used for a variety of purposes, such as:
- Bypassing authorization middleware for endpoints that are publicly accessible.
- Bypassing CORS middleware for endpoints that are only accessed by internal clients.
- Bypassing logging middleware for endpoints that do not generate any interesting logs.
- Improving performance for endpoints that are simple and do not need any middleware processing.
Here are some examples of when you might want to use Route Short Circuit middleware:
- A health check endpoint that simply returns a 200 OK response.
- A static content i.e. robots.txt endpoint that serves the robots.txt file.
- A favicon.ico endpoint that serves the favicon.ico file.
- An API endpoint that is only accessible to internal clients.
- An API endpoint that is used to generate images or other static content.
It is important to note that Route Short Circuit middleware should not be used for endpoints that need to perform any complex processing or that need to run middleware for security or compliance reasons. For example, you should not use Route Short Circuit middleware for endpoints that handle user authentication or sensitive data.💡