Finished 'Attribute Routing with Optional Parameter'

master
jason.zhu 2021-06-16 15:19:24 +10:00
parent 0eae1cbf18
commit 4dd245cf1f
4 changed files with 22 additions and 11 deletions

View File

@ -168,6 +168,8 @@
<Content Include="Views\Students\GetStudentByID.cshtml" />
<Content Include="Views\Students\GetStudentCourses.cshtml" />
<Content Include="Views\Students\GetAllStudents.cshtml" />
<Content Include="Views\Home\MVC.cshtml" />
<Content Include="Views\Home\WEBAPI.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View File

@ -8,22 +8,23 @@ namespace AttributeRoutingDemoInMVC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
// Optional URI Parameter
// URL: /MVCTest/
// URL: /MVCTest/Pranaya
[Route("MVCTest/{studentName ?}")]
public ActionResult MVC(string studentName)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
// Optional URI Parameter with default value
// URL: /WEBAPITest/
// URL: /WEBAPITest/Pranaya
[Route("WEBAPITest/{studentName = Pranaya}")]
public ActionResult WEBAPI(string studentName)
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
ViewBag.Message = "Welcome to ASP.NET WEB API!";
return View();
}
}

View File

@ -0,0 +1,4 @@
@{
ViewBag.Title = "MVC";
}
@ViewBag.Message

View File

@ -0,0 +1,4 @@
@{
ViewBag.Title = "WEBAPI";
}
@ViewBag.Message