Attribute Routing in WebAPI

Other topics

Remarks:

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.

Basic Attribute 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();
    }
}

Route Prefix Attribute

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 
}

}

Syntax:

  • [RoutePrefix("api/books")] - for controller class
  • [Route("getById")] - for actions
  • [Route("~/api/authors/{authorId:int}/books")] - for overriding route prefix

Parameters:

Parameter NameDetails
RoutePrefixattribute to the controller class. all common url prefixes in actions are clubbed here. takes string as input
Routeattribute to the controller actions. each action will have route assosciated with(not necessarily)
Route("~/api/")this overrides the Route Prefix

Contributors

Topic Id: 9440

Example Ids: 29247,32433

This site is not affiliated with any of the contributors.