25

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");
   }
}
1

1 Answer 1

50

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")]

1
  • 11
    Why is this the accepted answer? Multiple route prefixes could help avoid multiple [Route] attributes for each method and go a long way to helping ease maintenance on a large api controller Commented Jun 28, 2017 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.