Middleware that calls other middlewares
<p>I have some piece of ASP.NET Core code that I want to extract into a custom middleware. Specifically, the following logic should be implemented: when a certain path <code>mapPath</code> is requested, proxy the request to another host, identified by <code>proxyUrl</code>.</p>

<p>The following code in <code>Startup.cs</code> does the trick:</p>

<pre><code>var proxyUri = new Uri(proxyUrl);
builder.Map(
    mapPath,
    appMapped =&gt;
    {
        appMapped.RunProxy(
            new ProxyOptions
                {
                    Scheme = proxyUri.Scheme,
                    Host = proxyUri.Host,
                    Port = proxyUri.Port.ToString()
                });
    }
);
</code></pre>

<p>Well, it uses <code>app.Map()</code> to branch and then the Proxy middleware to proxy the request.</p>

<p>(How) Is it possible to extract this logic to a custom and resusable middleware? Or am I not able to use a "real" middleware here? What I can do is of course write an extension method, e.g. <code>app.UseMapProxy()</code> and put the logic 1:1 in there, but I just wondered if I can do it with a "real" middleware class as well.</p>