Inside the configure, I can attach a global middleware using:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
....
app.UseMiddleware<MyMiddleware>();
...
}
This will apply to all actions.
However, I thought to myself, how can I attach a middleware to a specific route/action? (Sure I can put some if's inside the code but I don't like the approach)
But then I saw this:
app.UseEndpoints(endpoints =>
{
endpoints.Map("/version", endpoints.CreateApplicationBuilder()
.UseMiddleware<MyMiddleware>()
.UseMiddleware<VersionMiddleware>()
.Build())
.WithDisplayName("Version number");
}
This will work but will create a NEW endpoint /version.
Question
How can I attach custom middleware to an existing controller action route?
I've tried:
endpoints.Map("/weatherforecast", endpoints.CreateApplicationBuilder()
.UseMiddleware<MyMiddleware>()
.UseMiddleware<VersionMiddleware>()
.Build())
.WithDisplayName("Version number");
But it doesn't seem to affect. I see a regular response from the controller. Without new headers which the middleware adds.
You need the MapWhen
https://www.devtrends.co.uk/blog/conditional-middleware-based-on-request-in-asp.net-core
from the link, modified:
app.UseMiddlewareOne();
app.MapWhen(context => context.Request.Path.StartsWithSegments("/version", StringComparison.OrdinalIgnoreCase)), appBuilder =>
{
appBuilder.UseMiddlewareTwo();
});
app.UseMiddlewareThree();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With