From 413db6c921fd180fc7020bf7191ba2d988f7e410 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Wed, 16 Jun 2021 16:17:18 +1000 Subject: [PATCH] Finished 'Attribute Route Constraints in ASP.NET MVC' --- .../AttributeRoutingDemoInMVC.csproj | 1 + .../Controllers/StudentsController.cs | 14 ++++++++++--- .../Views/Students/GetStudentDetails.cshtml | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 AttributeRoutingDemoInMVC/Views/Students/GetStudentDetails.cshtml diff --git a/AttributeRoutingDemoInMVC/AttributeRoutingDemoInMVC.csproj b/AttributeRoutingDemoInMVC/AttributeRoutingDemoInMVC.csproj index 2827d9c..41a33a6 100644 --- a/AttributeRoutingDemoInMVC/AttributeRoutingDemoInMVC.csproj +++ b/AttributeRoutingDemoInMVC/AttributeRoutingDemoInMVC.csproj @@ -172,6 +172,7 @@ + diff --git a/AttributeRoutingDemoInMVC/Controllers/StudentsController.cs b/AttributeRoutingDemoInMVC/Controllers/StudentsController.cs index b9788e2..5d05c21 100644 --- a/AttributeRoutingDemoInMVC/Controllers/StudentsController.cs +++ b/AttributeRoutingDemoInMVC/Controllers/StudentsController.cs @@ -29,16 +29,24 @@ namespace AttributeRoutingDemoInMVC.Controllers } [HttpGet] - [Route("{studentID}")] + [Route("{studentID:int:range(1,3)}")] // This will be translated to /students/2 - public ActionResult GetStudentByID(int studentID) + public ActionResult GetStudentDetails(int studentID) { Student studentDetails = students.FirstOrDefault(s => s.Id == studentID); return View(studentDetails); } [HttpGet] - [Route("students/{studentID}/courses")] + [Route("{studentName:alpha}")] + public ActionResult GetStudentDetails(string studentName) + { + Student studentDetails = students.FirstOrDefault(s => s.Name == studentName); + return View(studentDetails); + } + + [HttpGet] + [Route("{studentID}/courses")] // This will be translated to /students/2/courses public ActionResult GetStudentCourses(int studentID) { diff --git a/AttributeRoutingDemoInMVC/Views/Students/GetStudentDetails.cshtml b/AttributeRoutingDemoInMVC/Views/Students/GetStudentDetails.cshtml new file mode 100644 index 0000000..91d5637 --- /dev/null +++ b/AttributeRoutingDemoInMVC/Views/Students/GetStudentDetails.cshtml @@ -0,0 +1,21 @@ +@model AttributeRoutingDemoInMVC.Models.Student +@{ + ViewBag.Title = "GetStudentDetails"; +} + +

GetStudentDetails

+
+

Student

+
+
+
+ @Html.DisplayNameFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Name) +
+
+ @Html.DisplayFor(model => model.Id) +
+
+
\ No newline at end of file