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