Currently, Attribute Routes doesn't have Controller specific Message Handlers
. As there is no way to specify Which handler to execute for which route at the time of declaration. This is possible in Conventional Routing
.
Simply add an attribute to the controller action
[Route("product/{productId}/customer")]
public IQueryable<Product> GetProductsByCustomer(int productId)
{
//action code goes here
}
this will be queried as /product/1/customer
and productId=1
will be sent to the controller action.
Make sure the one within '{ }' and the action parameter are same. productId
in this case.
before using this, you have to specify that you are using Attribute Routing by:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
}
}
In cases where you need a common portion of the route for all routes within a controller, RoutePrefix
attribute is used.
In the below example, api/students part of the code is common and so we can define RoutePrefix
and avoid using it repeatedly.
[RoutePrefix("api/students")]
public class StudentController : ApiController
{
[Route("")]
public IEnumerable<Student> Get()
{
//action code goes here
}
[Route("{id:int}")]
public Student Get(int id)
{
//action code goes here
}
[Route("")]
public HttpResponseMessage Post(Student student)
{
//action code goes here
}
}
Parameter Name | Details |
---|---|
RoutePrefix | attribute to the controller class. all common url prefixes in actions are clubbed here. takes string as input |
Route | attribute to the controller actions. each action will have route assosciated with(not necessarily) |
Route("~/api/") | this overrides the Route Prefix |