Finished 'Attribute Route Constraints in ASP.NET MVC'

master
jason.zhu 2021-06-16 16:17:18 +10:00
parent e553d28b48
commit 413db6c921
3 changed files with 33 additions and 3 deletions

View File

@ -172,6 +172,7 @@
<Content Include="Views\Home\MVC.cshtml" /> <Content Include="Views\Home\MVC.cshtml" />
<Content Include="Views\Home\WEBAPI.cshtml" /> <Content Include="Views\Home\WEBAPI.cshtml" />
<Content Include="Views\Students\GetTeachers.cshtml" /> <Content Include="Views\Students\GetTeachers.cshtml" />
<Content Include="Views\Students\GetStudentDetails.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />

View File

@ -29,16 +29,24 @@ namespace AttributeRoutingDemoInMVC.Controllers
} }
[HttpGet] [HttpGet]
[Route("{studentID}")] [Route("{studentID:int:range(1,3)}")]
// This will be translated to /students/2 // 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); Student studentDetails = students.FirstOrDefault(s => s.Id == studentID);
return View(studentDetails); return View(studentDetails);
} }
[HttpGet] [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 // This will be translated to /students/2/courses
public ActionResult GetStudentCourses(int studentID) public ActionResult GetStudentCourses(int studentID)
{ {

View File

@ -0,0 +1,21 @@
@model AttributeRoutingDemoInMVC.Models.Student
@{
ViewBag.Title = "GetStudentDetails";
}
<h2>GetStudentDetails</h2>
<div>
<h4>Student</h4>
<hr/>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dd>
@Html.DisplayFor(model => model.Id)
</dd>
</dl>
</div>