Created
March 5, 2018 23:40
-
-
Save fernandezja/070599445f1ece2b2d2a93aa0968bf90 to your computer and use it in GitHub Desktop.
Controller ASP.NET Core With route attribute and anchor with TagHelper Route #aspnetcore asp-rute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model Starwars.App.Web.Entities.Jedi | |
@{ | |
ViewData["Title"] = "Details"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
<h2>Details</h2> | |
Jedi: <strong>@Model.Name</strong> | |
<ul> | |
<li> | |
<a asp-controller="Jedi" | |
asp-action="Details" | |
asp-route-name="@Model.Name"> | |
@Model.Name | |
</a> | |
</li> | |
</ul> | |
Another examples: | |
<ul> | |
<li> | |
<a asp-controller="Jedi" | |
asp-action="Details" | |
asp-route-name="Yoda"> | |
Yoda | |
</a> | |
</li> | |
<li> | |
<a asp-controller="Jedi" | |
asp-action="Details" | |
asp-route-name="Luke Skywalker"> | |
Luke Skywalker | |
</a> | |
</li> | |
</ul> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JediController : Controller | |
{ | |
[Route("Jedi/{name}")] | |
public IActionResult Details(string name) | |
{ | |
if (string.IsNullOrEmpty(name)) | |
{ | |
throw new ArgumentException("The name is required"); | |
} | |
var jedi = new Jedi() | |
{ | |
Id = 1, | |
Name = name | |
}; | |
return View(jedi); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment