title: Get Url from ApiController and Action names, in a project containing Controllers and ApiControllers
body: <p>An existing project has controllers that inherit from either:</p>
  
  <ul>
  <li><code>Controller</code>: <code>RouteTable.Routes.MapRoute</code> with <code>"{controller}/{action}/{id}"</code>.</li>
  <li><code>ApiController</code>: <code>GlobalConfiguration.Configure</code> and in the callback <code>MapRoute</code> with <code>"api/{controller}/{id}"</code>.</li>
  </ul>
  
  <p>Everything works fine, but I need to generate URLs for action methods in both of these types of controllers. Given:</p>
  
  <ol>
  <li>a name or type of a controller that inherits from either of these, and</li>
  <li>an action method name</li>
  </ol>
  
  <p>Then from the web site side, how can I generate proper URLs for the web API side?</p>
  
  <p>I'm using reflection to get action and controller names right now, and then through using <code>UrlHelper.Action(actionName, controllerName, routeValueDictionary)</code> am getting the correct URL for web site routes.</p>
  
  <p>However, this method is (of course) generating URLs like this for the WebAPI side: <code>/ApiControllerName/Get?parameter1=value</code> when it needs to be <code>/api/ApiControllerName?parameter1=value</code> and the separate knowledge that it's a GET request.</p>
  
  <p>Purpose: this is for a smoke test page for the web site that uses attributes and reflection to decide what to smoke test. It would be nice to be able to use the same attribute throughout the project, and furthermore it would be very nice to be able to use the correct <code>UrlHelper</code> that is aware of the routing tables and can produce the right prefix such as <code>/api/</code>, instead of the code assuming, perhaps wrongly, that the API routes were registered with <code>api</code> and not, say, <code>webapi</code>.</p>
  
  <p><strong>Update</strong></p>
  
  <p>After continued research I have found <a href="http://blogs.msdn.com/b/roncain/archive/2012/07/17/using-the-asp-net-web-api-urlhelper.aspx" rel="nofollow noreferrer">the <code>Url.HttpRouteUrl</code> method</a> which can generate WebAPI URLs, but this requires knowing a route name, not an action method name.</p>
  
  <p>I've done even more research on this and haven't gotten any closer to a solution. It looks like if you know the route name of the appropriate route, <a href="https://stackoverflow.com/a/15256986/57611">you can cook up a Url easily</a>. There are also some likely hints <a href="https://stackoverflow.com/questions/4748342/how-to-determine-if-an-arbitrary-url-matches-a-defined-route">here</a> and <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection" rel="nofollow noreferrer">here</a>. But if there are multiple routes for WebApi, how do you know which one matches the controller and action you want? It would be dumb to reimplement what MVC itself already does in selecting a controller and action. I guess I could construct a URL from the given parameters using every WebApi route, then run the URL through its paces (using some of the above links) and see if it is going to match the desired controller... yuck.</p>
  
  <p>There's got to be an easier way.</p>
  
  <p>For now I'm going to have to move on, but here's to hoping someone can help me out.</p>