Skip to content

Instantly share code, notes, and snippets.

@fernandezja
Created March 5, 2018 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezja/070599445f1ece2b2d2a93aa0968bf90 to your computer and use it in GitHub Desktop.
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
@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>
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