liveBook · Manning

Anchor tag helper

As explained in ASP.NET Core in Action, 3e

The Anchor Tag Helper is an ASP.NET Core Tag Helper that generates an anchor element's URL from Razor Page and routing attributes.

The Anchor Tag Helper generates an <a> element’s URL through Razor Pages routing, avoiding hardcoded href values. Set asp-page and add asp-page-handler or asp-route-* attributes when needed. section 18.3

Navigation links in _Layout.cshtml

Use <a asp-area="" asp-page="/Index">Home</a> and <a asp-area="" asp-page="/Privacy">Privacy</a>. Routing generates / and /Privacy, respectively. section 18.3

Customize URL generation with these attributes:

  • asp-page selects the Razor Page; asp-page-handler selects its handler. section 18.3
  • asp-route-* supplies route parameters, while asp-area selects an area. section 18.3
  • asp-host or asp-protocol creates an absolute URL with the requested host or HTTP protocol. section 18.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains core attributes, generated defaults, and URL customization, but defers detailed treatment of areas.
Examples
Uses _Layout.cshtml navigation links for the Index and Privacy Razor Pages.
What it assumes
Assumes Razor Page and routing basics while demonstrating link generation through simple navigation markup.
What you can do after
Apply it via Razor markup to generate maintainable links for pages, handlers, routes, areas, hosts, and protocols.
How it connects
Connects Razor Pages, routing, Razor markup, Tag Helpers, LinkGenerator, and IUrlHelper.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.