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
}
}