Added Route Prefix in Attribute Routing

master
jason.zhu 2021-06-16 15:37:30 +10:00
parent 4dd245cf1f
commit a4d2fda2a9
1 changed files with 6 additions and 0 deletions

View File

@ -9,6 +9,7 @@ using AttributeRoutingDemoInMVC.Models;
namespace AttributeRoutingDemoInMVC.Controllers namespace AttributeRoutingDemoInMVC.Controllers
{ {
[RoutePrefix("students")]
public class StudentsController : Controller public class StudentsController : Controller
{ {
private static List<Student> students = new List<Student>() private static List<Student> students = new List<Student>()
@ -20,12 +21,16 @@ namespace AttributeRoutingDemoInMVC.Controllers
}; };
[HttpGet] [HttpGet]
[Route]
// This will be translated to /students
public ActionResult GetAllStudents() public ActionResult GetAllStudents()
{ {
return View(students); return View(students);
} }
[HttpGet] [HttpGet]
[Route("{studentID}")]
// This will be translated to /students/2
public ActionResult GetStudentByID(int studentID) public ActionResult GetStudentByID(int studentID)
{ {
Student studentDetails = students.FirstOrDefault(s => s.Id == studentID); Student studentDetails = students.FirstOrDefault(s => s.Id == studentID);
@ -34,6 +39,7 @@ namespace AttributeRoutingDemoInMVC.Controllers
[HttpGet] [HttpGet]
[Route("students/{studentID}/courses")] [Route("students/{studentID}/courses")]
// This will be translated to /students/2/courses
public ActionResult GetStudentCourses(int studentID) public ActionResult GetStudentCourses(int studentID)
{ {
List<string> CourseList = new List<string>(); List<string> CourseList = new List<string>();