13

If I decorate this web api controller with the Route attribute I can hit the method

[Route("api/v{version}/bank-accounts")]
public class BankAccountsController : ApiController
{
    [HttpGet]
    public HttpResponseMessage GetBankAccounts()
    {
        //...
    }
}

But if I use RoutePrefix instead I can't make it work unless at the same time I use Route("")

[RoutePrefix("api/v{version}/bank-accounts")]
public class BankAccountsController : ApiController
{
    [HttpGet]
    [Route("")]
    public HttpResponseMessage GetBankAccounts()
    {
        //...
    }
}

Is this intended, or I'm messing things?

Thanks

Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
mitomed
  • 2,006
  • 2
  • 29
  • 58

2 Answers2

22

Right, this is an expected behavior... RoutePrefix attribute by itself doesn't add any routes to the route table where as Route attributes do...

Kiran
  • 56,921
  • 15
  • 176
  • 161
7

You are missing it... The route prefix, is just that, a prefix. You should move part of the path template to the route attribute. Like this.

[RoutePrefix("api/v{version}")]
public class BankAccountsController : ApiController
{
    [HttpGet]
    [Route("bank-accounts")]
    public HttpResponseMessage GetBankAccounts(string version)
    {
        //...
    }
}
John C
  • 1,761
  • 2
  • 20
  • 30