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
Route prefixes are associated with routes by design in attribute routing. It is used to set a common prefix for an entire controller.
Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.
Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources.
We can override the RoutePrefix on a method, using the tilde(~) sign. We can easily define parameterized templates in the attribute based routing. We can have parameters defined in-between the API request URLs.
Right, this is an expected behavior... RoutePrefix
attribute by itself doesn't add any routes to the route table where as Route
attributes do...
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)
{
//...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With