title: Register Area and Controllers Dynamically
body: <p>I've a modular web application and I need to load some libraries on runtime, those libraries will contain <code>Controllers</code>.</p>
  
  <p>Each controller must have a single <code>Area</code> and it's common to all controllers inside that library.</p>
  
  <p><a href="https://i.stack.imgur.com/uqCIJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uqCIJ.png" alt="Example1"></a></p>
  
  <p>I've used ASP.NET Core's application parts to load this assembly during runtime.</p>
  
  <pre><code>services.AddMvcCore(setup =&gt;
  {
      setup.Filters.Add(new AuthorizeFilter());
  
      // Get all global filters
      foreach (var filter in GetInterfacesFromAssembly&lt;IGlobalAsyncFilter&gt;())
          setup.Filters.Add(new AsyncActionFilterProxy(filter, container));
  })
  .SetCompatibilityVersion(CompatibilityVersion.Latest)
  .AddFormatterMappings()
  .AddJsonFormatters()
  .AddCors()
  .AddAuthorization(o =&gt;
  {
      o.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
       .RequireAuthenticatedUser()
       .Build();
  })
  .AddApplicationPart(GetModuleAssemblies());
  </code></pre>
  
  <p><code>GetModuleAssemblies</code> gets all the possible libraries called in runtime that may contain <code>Controllers</code></p>
  
  <pre><code>/// &lt;summary&gt;
  /// Get any DLL that contains "ModularPortal.Modules.*.dll"
  /// &lt;/summary&gt;
  private static IEnumerable&lt;Assembly&gt; GetModuleAssemblies()
  {
      string location = Assembly.GetEntryAssembly().Location;
      string path = Path.GetDirectoryName(location);
      DirectoryInfo directory = new DirectoryInfo(path);
  
      FileInfo[] files = directory.GetFiles("ModularPortal.Module*.dll");
      foreach (FileInfo file in files)
          yield return Assembly.Load(file.Name.Replace(".dll", ""));
  }
  </code></pre>
  
  <p>Using this, the following controller is located in <code>/api/example1/test</code>:</p>
  
  <pre><code>[AllowAnonymous]
  public class Example1Controller : ControllerBase
  {
      readonly IMembership membership;
  
      public Example1Controller(
              IMembership membership
          )
      {
          this.membership = membership;
      }
  
      [HttpGet]
      public IActionResult Test()
      {
          return Ok("t1");
      }
  }
  </code></pre>
  
  <p>The problem is that by adding <code>ApplicationPart</code> I'll need to use the <code>AreaAttribute</code> to identity that the controller belongs to <code>/Area</code>.</p>
  
  <p>I want a generic "Module Configurator" that any controller inside that library WILL have a common area.</p>
  
  <p>I started by creating an interface to identity the module:</p>
  
  <pre><code>public interface IModule
  {
      string DisplayName { get; }
      string RouteName { get; }
  
      void Configure(ModuleConfiguration config);
  }
  </code></pre>
  
  <p>Then inside the library, I used it:</p>
  
  <pre><code>public class Module : IModule
  {
      public string DisplayName =&gt; "Example Module";
  
      public string RouteName =&gt; "Ex";
  
      public void Configure(ModuleConfiguration config)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>I want to use this code to configure everything about that module, including Area and controller registration.</p>
  
  <p>In the example above, after registering an Area the path should be <code>/api/ex1/example1/test</code>.</p>
  
  <p>This will only be possible if I, somehow, changed the <code>AddApplicationPart</code> to register it myself.</p>
  
  <p>Any idea?</p>