I have added a custom middleware to my ASP.NET Core Web-API 2.1 application, which needs to be executed only for certain requests. The problem is, that it is always executed in the pipeline.
Startup.cs
app.UseWhen(context => context.Request.Path.Value.Contains("AWS"), appBuilder =>
{
app.UseMiddleware<ValidateHeaderHandler>();
});
The code from above completely ignores the condition and always executes the ValidateHeaderHandler
middleware.
You need to call the UseMiddleware()
method on the appBuilder
object, not on app
directly:
app.UseWhen(context => context.Request.Path.Value.Contains("AWS"), appBuilder =>
{
appBuilder.UseMiddleware<ValidateHeaderHandler>();
});
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