ASP.NET Web API multiple RoutePrefix

Bhalchandra K picture Bhalchandra K · Jul 25, 2014 · Viewed 11.7k times · Source

The opensource Attribute Routing allows to have multiple route-prefixes. Why does ASP.NET Web API 2.0 does not allow to have multiple RoutePrefix().

[RoutePrefix("api/v1/{abc}/Entity")]
[RoutePrefix("api/v1/{abc}/{xyz?}/Entity")]
public class MyApiController : ApiController
{
   [Route("")]
   public IHttpResult Get()
   {
      return Ok("Hello World");
   }
}

Answer

jaxxbo picture jaxxbo · Mar 9, 2015

You can add a route to the action method also overriding the RoutePrefix with a "~"

example:

[RoutePrefix("api/v1/{abc}/Entity")]
public class MyApiController : ApiController
{
   [Route("")]
   [Route("~/api/v1/{abc}/{xyz?}/Entity")]
   public IHttpResult Get()
   {
      return Ok("Hello World");
   }
}

Notice the line: [Route("~/ api/v1/{abc}/{xyz?}/Entity")]