title: How to inject controller into WebApp
body: <p>I am building a library that is supposed to add features into an aspnetcore WebApp.</p>
  <p>My goal is to add Controllers written in the library to respond to requests on specific routes on the WebApp itself.</p>
  <p>The first thing I've done is to write an extension method as such:</p>
  <pre><code>public static IApplicationBuilder UseCustomLibrary(this IApplicationBuilder app) 
  {
    if (app == null) 
    {
      throw new ArgumentNullException(nameof(app));
    }
    //Here I want to hookup the routes to respond using my controller
  }
  </code></pre>
  <p>Given that piece of code, I can now run <code>app.UseCustomLibrary()</code> from <code>Program.cs</code>.</p>
  <p>Now, hooking up the actual controller to respond turns out to be not an easy task.</p>
  <p>Things I've tried:</p>
  <ul>
  <li><p>Using Custom Middleware:</p>
  <pre><code>  public class MyCustomMiddleware
    {
        private readonly RequestDelegate _next;
  
        public MyCustomMiddleware(RequestDelegate next)
        {
            _next = next ?? throw new ArgumentNullException(nameof(next));
        }
  
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Path.Value.Contains(&quot;expectedpath&quot;))
            {
                //Manually handle the request.
            }
  
            await _next(context);
        }
    }
  </code></pre>
  </li>
  </ul>
  <p>Even though that kind of works, that doesn't really hookup the controller. Instead I can handle the request manually. I dislike that because I now have to rewrite the controller into some kind of handler. That forces to abandon the nice controller structure of automatically determining the route using area/controller/action pattern. So this is currently my plan B.</p>
  <ul>
  <li><p>Using Custom IApplictionFeatureProviders:</p>
  <pre><code>  internal class MyCustomFeatureProvider : IApplicationFeatureProvider&lt;ControllerFeature&gt;
    {
        public void PopulateFeature(IEnumerable&lt;ApplicationPart&gt; parts, ControllerFeature feature)
        {
            if (!feature.Controllers.Contains(typeof(CustomController).GetTypeInfo()))
            {
                feature.Controllers.Add(typeof(CustomController).GetTypeInfo());
            }
        }
    }
  </code></pre>
  </li>
  </ul>
  <p>Then I would slightly modify the extension method to be call that provider as such:</p>
  <pre><code>public static IApplicationBuilder UseCustomLibrary(this IApplicationBuilder app)
  {
      app.Services.AddMvc().PartManager.FeatureProviders.Add(new MyCustomFeatureProvider());
      return builder;
  }
  </code></pre>
  <p>I was trying to copy what Microsoft.Identity library does in how it adds the &quot;AccountController&quot;. Unfortunately that also doesn't work. The controller actions are never called for some reason.</p>
  <p>I have also tried using that in conjunction with the middleware, by replacing the invoke method of the middleware with the following:</p>
  <pre><code>public async Task Invoke(HttpContext context)
  {
      context.Features.Set&lt;IApplicationFeatureProvider&lt;ControllerFeature&gt;&gt;(new MyCustomFeatureProvider());
  
      await _next(context);
  }
  </code></pre>
  <p>Again, that simply does nothing and the controller actions are never called.</p>
  <p>I've also tried calling <code>app.MapControllerRoute</code> from my extension method, but it doesn't work as well for a reason that I cannot understand. It seems the controller is not even registered so it is never called.</p>
  <p>I am willing to provide more details. I know there must be a way to do that, since it is used all the time in Microsoft.Identity and Microsoft.AspNetCore.Authentication to respond to authentication callbacks from IdPs for example. So there must be a way to do what I want.</p>