asp.net-web-api Attribute Routing in WebAPI Route Prefix Attribute

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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 
}

}


Got any asp.net-web-api Question?