title: ASP.NET Core - Configurable routing
body: <p>I have an ASP.NET Core 3.1 app. This app has an &quot;Admin&quot; area. I've put this area in it's own library to use in other apps. In my other app, I can currently access the &quot;Admin&quot; area by visiting <code>https://{domain}/_admin</code>. I want to reuse/share this library with other apps in my company. Now, other devs in my company have built other apps. So, I expect the <code>_admin</code> path may cause conflicts. For that reason, I want to make that part of the route configurable. Currently, I have the following</p>
  <p><strong>appsettings.json</strong></p>
  <pre><code>{
    &quot;Miscellaneous&quot;: {
      &quot;AdminRoot&quot;:&quot;_admin&quot;
    }
  }
  </code></pre>
  <p><strong>Startup.cs</strong></p>
  <pre><code>...
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
    ...
    app.UseEndpoints(endpoints =&gt;
    {
      var adminRoot = Configuration[&quot;Miscellaneous:AdminRoot&quot;];
      adminRoot = String.IsNullOrEmpty(adminRoot) ? &quot;_admin&quot; : adminRoot;
  
      endpoints.MapAreaControllerRoute(
        name: &quot;Admin&quot;,
        areaName: &quot;Admin&quot;,
        pattern: (adminRoot + &quot;/{controller=Home}/{action=Index}/{id?}&quot;)
      );
    }
    ...
  }
  ...
  </code></pre>
  <p><strong>AdminController.cs</strong></p>
  <pre><code>[Area(&quot;Admin&quot;)]
  public class HomeController : Controller
  {
    public IActionResult Index()
    {
      return View();
    }
  
    [Route(&quot;~/api/user-permissions&quot;)]
    public IActionResult LoadUserPermissions()
    {
      var permissions = GetPermissions();
      return Json(permissions);
    }
  }
  </code></pre>
  <p>This approach uses [convention-based routing][1]. At the same time, my Admin tool also has it's own REST API. For that reason, I want to mix convention-based routing with [attribute routing][2]. Basically, I want to have routes like <code>/_admin/api/user-permissions</code>. As this route demonstrates, the <code>_admin</code> creates a challenge if I add a <code>Route</code> attribute in my API since it's a configured value.</p>
  <p>I thought if I used the <code>~</code> it would be relative to the area. However, I get a 404 when I try this approach. Is there a way to dynamically set <em>parts</em> of a route? For example, I would love to be able to either:</p>
  <ul>
  <li>Use an approach relative to the area or</li>
  <li>Use an attribute like <code>[Route(&quot;{Configuration[&quot;Miscellaneous:AdminRoot&quot;]}/api/user-permissions&quot;)]</code></li>
  </ul>
  <p>I'm not finding a way to accomplish what I'm trying to do. Is there a way to do this? Please note, my Admin area is much more complex. I've tried to isolate this question to my actual problem. In addition, I'm looking to have a configurable approach for the sake of learning. For that reason, I'm not interested in solutions like changing &quot;_admin&quot; to a random value like a GUID or something. Thank you for your help!</p>