Using Route instead of RoutePrefix at controller level in .net web api attribute routing

mitomed picture mitomed · Nov 21, 2013 · Viewed 10.3k times · Source

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

Answer

Kiran Challa picture Kiran Challa · Nov 21, 2013

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