Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.