Finished 'Route Prefix in Attribute Routing'

This commit is contained in:
jason.zhu 2021-06-16 15:50:26 +10:00
parent a4d2fda2a9
commit e553d28b48
4 changed files with 57 additions and 0 deletions

View File

@ -126,6 +126,7 @@
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\Student.cs" />
<Compile Include="Models\Teacher.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@ -170,6 +171,7 @@
<Content Include="Views\Students\GetAllStudents.cshtml" />
<Content Include="Views\Home\MVC.cshtml" />
<Content Include="Views\Home\WEBAPI.cshtml" />
<Content Include="Views\Students\GetTeachers.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View File

@ -54,5 +54,19 @@ namespace AttributeRoutingDemoInMVC.Controllers
ViewBag.CourseList = CourseList;
return View();
}
[Route("~/tech/teachers")]
// This will be translated to /tech/teachers
public ActionResult GetTeachers()
{
List<Teacher> teachers = new List<Teacher>()
{
new Teacher() {Id = 1, Name = "James"},
new Teacher() {Id = 2, Name = "Patrik"},
new Teacher() {Id = 3, Name = "Smith"}
};
return View(teachers);
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AttributeRoutingDemoInMVC.Models
{
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,28 @@

@model IEnumerable<AttributeRoutingDemoInMVC.Models.Teacher>
@{
ViewBag.Title = "Get Teachers";
}
<h2>GetTachers</h2>
<teacher class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</teacher>