liveBook · Manning

Anchor element

As explained in Pro ASP.NET Core 7, Tenth Edition

An anchor element is an HTML element that creates a hyperlink to a specified resource or URL.

An anchor element creates a hyperlink and is the basic mechanism for navigating through an application with HTTP GET requests. In ASP.NET Core, AnchorTagHelper transforms its href using the routing system, avoiding hard-coded URLs and reflecting routing changes automatically. section 26.3

Configure the destination with tag-helper attributes:

  • Use asp-action and asp-controller to target a controller action. section 26.3
  • Supply route-segment values with asp-route-[name], such as asp-route-id. section 26.3
  • Use asp-page for a Razor Page and asp-page-handler for its handler method. section 26.3.1
  • Add asp-fragment, asp-host, or asp-protocol when the generated URL needs those components. section 26.3

Linking a product row to an action

<a asp-action="index" asp-controller="home" asp-route-id="@Model?.ProductId" class="btn btn-sm btn-info text-white">Select</a> For product ID 3, rendering produces href="/Home/index/3". Clicking the anchor sends a GET request to the selected controller action with the route value available as its id argument. section 26.3

Linking to a Razor Page

<a asp-page="/suppliers/list" class="btn btn-secondary">Suppliers</a> The helper resolves the page route, including any route convention or @page override, instead of requiring the rendered path to be written manually. section 26.3.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains attribute selection, route values, rendered hrefs, browser verification, and URL generation, but not tag-helper internals.
Examples
Uses product-list rows, Select links, Suppliers links, route identifiers, rendered hrefs, and a Razor Pages product listing.
What it assumes
Assumes familiarity with ASP.NET Core routing, controllers, Razor Pages, views, actions, handlers, and route segments.
What you can do after
Builds routing-aware links and verifies their rendered URLs and GET behavior in a browser.
How it connects
Connects anchor elements to tag helpers, routing, controller actions, Razor Pages, page handlers, and URL helpers.

Also explained in

Compare how 4 books explain anchor element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.