Reputation: 1079
I was wondering that if we use RoutePrefix
attribute in our web api controller with a different name from controller's actual name. So would it work or not?
As far as i did
[RouterPrefix("quotation")]
public class SaleOrderController : ApiController { ... }
if we define RoutePrefix
like above we can't access it via /quotation
but we can access it using saleorder
.
So what is RoutePrefix
for or am i doing something wrong ?
Upvotes: 6
Views: 11994
Reputation: 247088
To use default route use Route("")
[RoutePrefix("quotation")]
public class SaleOrderController : ApiController {
//GET quotation
[Route("")]
[HttpGet]
public IHttpActionResult GetAll() { ... }
}
Source: Attribute Routing in ASP.NET Web API 2 : Route Prefix
Upvotes: 6
Reputation: 10695
In order for it to work, you need to call the code below inside your WebApiConfig.Register()
method:
config.MapHttpAttributeRoutes();
So your RoutePrefix
works as exptected:
[RoutePrefix("quotation")]
public class SaleOrderController : ApiController
{
[Route("example")]
[HttpGet]
public IHttpActionResult Example()
{
return Ok();
}
[Route("another")]
[HttpGet]
public IHttpActionResult Another()
{
return Ok();
}
}
So your could access your apis like this:
Upvotes: 4