title: Use .net core middleware with .Map configured to a specific path
body: <p>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.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2#use-run-and-map" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2#use-run-and-map</a></p>
  
  <p>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:</p>
  
  <pre><code> 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&lt;CustomMiddleware&gt;();
          }
      }
  </code></pre>
  
  <p>What I can't see is how to combine the two. One thing I tried was this:</p>
  
  <pre><code>public static class CustomMiddlewareExtensions
      {
          public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
          {
              return builder.Map("/CustomPath", app =&gt; { 
                  app.UseMiddleware&lt;CustomMiddleware&gt;(); });
          }
      }
  </code></pre>
  
  <p>But this is apparently not working as it is calling <code>InvokeAsync</code> for everything.</p>
  
  <p>I could easily look at the HttpContext.Request.Path and skip my <code>do stuff</code> if it does not match but wanted to know if it was possible to use .Map before doing so.</p>