RoutePrefix attribute in Web API
Text version of the video
http://csharp-video-tutorials.blogspot.com/2017/02/routeprefix-attribute-in-web-api.html
Slides
http://csharp-video-tutorials.blogspot.com/2017/02/routeprefix-attribute-in-web-api_15.html
All ASP .NET Web API Text Articles and Slides
http://csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-tutorial-for-beginners.html
All ASP .NET Web API Videos
https://www.youtube.com/playlist?list=PL6n9fhu94yhW7yoUOGNOfHurUE6bpOO2b
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
In this video we will discuss the use of RoutePrefix attribute with an example.
RoutePrefix attribute : As you can see from the example below, all the routes in the StudentsController start with the same prefix - api/students
public class StudentsController : ApiController
{
[Route("api/students")]
public IEnumerable[Student] Get()
[Route("api/students/{id}")]
public Student Get(int id)
[Route("api/students/{id}/courses")]
public IEnumerable[string] GetStudentCourses(int id)
}
The common prefix "api/students" can be specified for the entire controller using the [RoutePrefix] attribute as shown below. This eliminates the need to repeat the common prefix "api/students" on every controller action method.
[RoutePrefix("api/students")]
public class StudentsController : ApiController
{
[Route("")]
public IEnumerable[Student] Get()
[Route("{id}")]
public Student Get(int id)
[Route("{id}/courses")]
public IEnumerable[string] GetStudentCourses(int id)
}
However, sometimes you may want to override the route prefix. Let us understand this with an example.
Right click on the models folder, and add a new class file. Name it "Teacher.cs". Copy and paste the following code.
namespace WebAPI.Models
{
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Add the following GetTeachers() method to the "StudentsController".
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
We want GetTeachers() method to be mapped to URI "/api/teachers".
[Route("api/teachers")]
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
If we use the [Route] attribute on GetTeachers() method as shown above and when we navigate to /api/teachers, we get the following error.
No HTTP resource was found that matches the request URI 'http://localhost:65116/api/teachers'.
But if we navigate to /api/students/api/teachers then we get the list of teachers. This is because of the [RoutePrefix("api/students")] attribute on StudentsController. So there is definitely a need to override the RoutePrefix used on the StudentsController. To override the RoutePrefix use ~ as shown below
[Route("~/api/teachers")]
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
With this change GetTeachers() action method is mapped to URI "/api/teachers" as expected.
What is the use of RoutePrefix attribute
RoutePrefix attribute is used to specify the common route prefix at the controller level to eliminate the need to repeat that common route prefix on every controller action method
How to override the route prefix
Use ~ character to override the route prefix
Видео RoutePrefix attribute in Web API канала kudvenkat
http://csharp-video-tutorials.blogspot.com/2017/02/routeprefix-attribute-in-web-api.html
Slides
http://csharp-video-tutorials.blogspot.com/2017/02/routeprefix-attribute-in-web-api_15.html
All ASP .NET Web API Text Articles and Slides
http://csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-tutorial-for-beginners.html
All ASP .NET Web API Videos
https://www.youtube.com/playlist?list=PL6n9fhu94yhW7yoUOGNOfHurUE6bpOO2b
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
In this video we will discuss the use of RoutePrefix attribute with an example.
RoutePrefix attribute : As you can see from the example below, all the routes in the StudentsController start with the same prefix - api/students
public class StudentsController : ApiController
{
[Route("api/students")]
public IEnumerable[Student] Get()
[Route("api/students/{id}")]
public Student Get(int id)
[Route("api/students/{id}/courses")]
public IEnumerable[string] GetStudentCourses(int id)
}
The common prefix "api/students" can be specified for the entire controller using the [RoutePrefix] attribute as shown below. This eliminates the need to repeat the common prefix "api/students" on every controller action method.
[RoutePrefix("api/students")]
public class StudentsController : ApiController
{
[Route("")]
public IEnumerable[Student] Get()
[Route("{id}")]
public Student Get(int id)
[Route("{id}/courses")]
public IEnumerable[string] GetStudentCourses(int id)
}
However, sometimes you may want to override the route prefix. Let us understand this with an example.
Right click on the models folder, and add a new class file. Name it "Teacher.cs". Copy and paste the following code.
namespace WebAPI.Models
{
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Add the following GetTeachers() method to the "StudentsController".
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
We want GetTeachers() method to be mapped to URI "/api/teachers".
[Route("api/teachers")]
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
If we use the [Route] attribute on GetTeachers() method as shown above and when we navigate to /api/teachers, we get the following error.
No HTTP resource was found that matches the request URI 'http://localhost:65116/api/teachers'.
But if we navigate to /api/students/api/teachers then we get the list of teachers. This is because of the [RoutePrefix("api/students")] attribute on StudentsController. So there is definitely a need to override the RoutePrefix used on the StudentsController. To override the RoutePrefix use ~ as shown below
[Route("~/api/teachers")]
public IEnumerable[Teacher] GetTeachers()
{
List[Teacher] teachers = new List[Teacher]()
{
new Teacher() { Id = 1, Name = "Rob" },
new Teacher() { Id = 2, Name = "Mike" },
new Teacher() { Id = 3, Name = "Mary" }
};
return teachers;
}
With this change GetTeachers() action method is mapped to URI "/api/teachers" as expected.
What is the use of RoutePrefix attribute
RoutePrefix attribute is used to specify the common route prefix at the controller level to eliminate the need to repeat that common route prefix on every controller action method
How to override the route prefix
Use ~ character to override the route prefix
Видео RoutePrefix attribute in Web API канала kudvenkat
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Web API attribute routing constraints](https://i.ytimg.com/vi/QjmzK6m4txw/default.jpg)
![Attribute routing in ASP NET Web API 2](https://i.ytimg.com/vi/jvnQavMzfe4/default.jpg)
![Part 3 Why and when should we use an abstract class](https://i.ytimg.com/vi/yyU3bXyc_oU/default.jpg)
![Entity Framework Best Practices - Should EFCore Be Your Data Access of Choice?](https://i.ytimg.com/vi/qkJ9keBmQWo/default.jpg)
![Generating links using route names in asp net web api](https://i.ytimg.com/vi/sqOw6E11AAM/default.jpg)
![Angular 2 http service tutorial](https://i.ytimg.com/vi/DETRfgZGuk8/default.jpg)
![7# Asp.Net Core 2.0 MVC - Routing](https://i.ytimg.com/vi/JZNNrcP5GLY/default.jpg)
![Configure ASP NET Core request processing pipeline](https://i.ytimg.com/vi/nt6anXAwfYI/default.jpg)
![IHttpActionResult vs HttpResponseMessage](https://i.ytimg.com/vi/cNR7gDPJtQI/default.jpg)
![Part 8 Data access in mvc using entity framework](https://i.ytimg.com/vi/Lrr66APUwBk/default.jpg)
![What is ASP NET Web API](https://i.ytimg.com/vi/0pcM6teVdKk/default.jpg)
![ASP NET core project file](https://i.ytimg.com/vi/6gnsUsjRTVo/default.jpg)
![Creating a Web API Project](https://i.ytimg.com/vi/6qwuFQDB2jU/default.jpg)
![HTTP GET PUT POST DELETE](https://i.ytimg.com/vi/GbKBcDX8DDQ/default.jpg)
![Attribute Routing in ASP NET Core MVC](https://i.ytimg.com/vi/prNptonJAiY/default.jpg)
![Implementing post method in ASP NET Web API](https://i.ytimg.com/vi/0eGUix3Nkjg/default.jpg)
![Can we overload MVC Action methods ? ( MVC interview questions)](https://i.ytimg.com/vi/EaUUe3ndGi8/default.jpg)
![Web API - Creating a Controller with a Parameter](https://i.ytimg.com/vi/0g3g2h2VpOo/default.jpg)
![ASP NET Web API token authentication](https://i.ytimg.com/vi/gkWb7yQb6ro/default.jpg)