title: How can I manually instantiate a controller's attribute based route?
body: <p>I am implementing a web API that will return a 202 Accepted to the client when a resource is not yet available. The URL returned to the client would look something like</p>
  
  <p><code>http://host/api/v2/**thing**reqests/*guid*</code></p>
  
  <p>I want to do this generically as I have many different types of resources (and hence controllers) and I do not want to duplicate code. </p>
  
  <p>I created</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class)]
  public class RelatedControllerAttribute : Attribute
  {
      public Type RelatedControllerType { get; }
  
      public RelatedControllerAttribute(Type relatedControllerType) =&gt; RelatedControllerType = relatedControllerType;
  }
  </code></pre>
  
  <p>and applied it to the main controller like so</p>
  
  <pre><code>[RelatedController(typeof(ThingRequestsController))]
  public class ThingsController : ApiController&lt;ThingRequest&gt;
  </code></pre>
  
  <p>This allows me to get to the route template associated with the requests controller.</p>
  
  <pre><code>[Route("api/v{version:apiVersion}/[controller]")]
  </code></pre>
  
  <p>with this code in a base class of the ThingsController</p>
  
  <pre><code>    private string GetRequestsRoute()
      {
          if (_requestsPath != null)
              return _requestsPath;
  
          var a = GetType().GetCustomAttribute&lt;RelatedControllerAttribute&gt;();
          if (a == null)
              throw new NotSupportedException();
          var routeTemplate = a.RelatedControllerType.GetCustomAttribute&lt;RouteAttribute&gt;().RouteTemplate;
  
          return _requestsPath = route.Name;
      }
  </code></pre>
  
  <p>This gets me almost all the way there. How do I instantiate the route template with the correct meaningful values. Is there something built into ASP.NET Core? I can easily do the <code>[controller]</code> part of the route, but how do I do the <code>{version:ApiVersion}</code> part (that comes from the ApiExplorer)?</p>