IApplicationBuilder has a .Map method that all the examples online show you how to use when you are inlining your middleware configuration and using .Run or .Use directly.
Most of the examples then go on to say how this is a bad idea (rightly so for maintenance reasons alone) and show you how to make a middle ware component which looks something like this:
public class CustomMiddleware: IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
... do stuff.
await next(context);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CustomMiddlewareExtensions
{
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CustomMiddleware>();
}
}
What I can't see is how to combine the two. One thing I tried was this:
public static class CustomMiddlewareExtensions
{
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
{
return builder.Map("/CustomPath", app => {
app.UseMiddleware<CustomMiddleware>(); });
}
}
But this is apparently not working as it is calling InvokeAsync for everything.
I could easily look at the HttpContext.Request.Path and skip my do stuff if it does not match but wanted to know if it was possible to use .Map before doing so.