In ASP.NET Core MVC, how do the anchor and form tag helpers turn asp-controller, asp-action and asp-route-* attributes into a URL?
answer
- the route table, not a string
- ambient controller and action
- asp-route-{name}: segment or query string
- literal href plus asp-* throws
- asp-area="" leaves the current area
basics
~20 sBoth helpers pass asp-controller, asp-action, asp-area and each asp-route-{name} value to MVC's URL generation; a value matching a route parameter fills that segment, any other becomes a query-string pair, and an omitted controller or action reuses the current one.
solid answer
~40 sThe anchor tag helper (`<a>`) and the form tag helper (`<form>`) build `href` or `action` by handing `asp-controller`, `asp-action`, `asp-area` and the `asp-route-*` values to MVC's URL helper, so links follow the route table instead of hard-coded paths. Each `asp-route-{name}` is one route value: with the default `{controller=Home}/{action=Index}/{id?}` route, `asp-route-id="7"` fills the `{id}` segment, while `asp-route-ref="spring"` has no matching parameter and becomes `?ref=spring`. Omitting `asp-controller` or `asp-action` reuses the current request's values, which is why the template writes `asp-area=""` on its navigation links. `asp-all-route-data` takes a dictionary, `asp-fragment` appends `#...`, and `asp-route` targets a named route. A literal `href` or `action` together with any `asp-*` route attribute throws `InvalidOperationException`, and a form generated this way gets `method="post"` unless you set one.
code
razor · 13 lines@model PlantDetailsViewModel
<a asp-controller="Plants" asp-action="Details"
asp-route-id="@Model.Id" asp-route-ref="spring"
asp-fragment="care">Care guide</a>
@* href="/Plants/Details/7?ref=spring#care" *@
<form asp-controller="Basket" asp-action="Add" asp-route-plantId="@Model.Id">
<input asp-for="Quantity" />
<button type="submit">Add to basket</button>
<button asp-action="SaveForLater">Save for later</button>
</form>
@* action="/Basket/Add?plantId=7" method="post" *@go deeper
Recall the attribute names and that asp-route-id fills the id segment of the default route while unknown names become query-string values.
Explain ambient values, why the template writes asp-area="", and the form helper's defaults: generated action, method post, and formaction on buttons.
Diagnose empty hrefs and render-time InvalidOperationException from conflicting attributes, and keep links route-driven so route changes never break views.
Set conventions for links across areas and route names so a growing MVC app can restructure routes without a sweep through every view.
## What the two helpers do `AnchorTagHelper` targets `<a>` elements that carry any of its `asp-` attributes, and `FormTagHelper` targets `<form>`. Both collect the attributes into a **route value dictionary** and ask MVC's URL helper for a URL, then write it into `href` or `action`. The point is that the view never repeats a path: rename a controller's route and every link follows. ## Route values: segment or query string Each `asp-route-{name}="value"` adds one route value. What happens to it depends on the route template that generates the URL. With the conventional route `{controller=Home}/{action=Index}/{id?}`: | Markup | Generated `href` | |---|---| | `<a asp-controller="Plants" asp-action="Details" asp-route-id="7">` | `/Plants/Details/7` | | `<a asp-controller="Plants" asp-action="Details" asp-route-id="7" asp-route-ref="spring">` | `/Plants/Details/7?ref=spring` | | `<a asp-controller="Plants" asp-action="Index">` | `/Plants` (the default action is dropped) | | `<a asp-controller="Home" asp-action="Index">` | `/` | - A value whose name matches a template parameter fills that segment. - Any other value is appended as a **query-string** pair. - `asp-all-route-data="@routeData"` takes an `IDictionary<string, string>` when the set of values is built in code. ## Ambient values and areas When `asp-controller` or `asp-action` is missing, URL generation reuses the **ambient** values of the current request. On a view rendered by `PlantsController`, `<a asp-action="Details" asp-route-id="7">` links to `/Plants/Details/7`. The same applies to the area: a link rendered inside an `Admin` area keeps `area=Admin` unless you say otherwise, which is why the MVC template's layout writes `asp-area=""` on its navigation links so they always reach the root controllers. `asp-area="Admin"` targets an area explicitly. ## The other attributes - `asp-fragment="care"` appends `#care` to the URL. - `asp-protocol="https"` and `asp-host="shop.example"` make the URL absolute. - `asp-route="PlantDetails"` uses the route **named** `PlantDetails` instead of controller and action. - On a form, `<button asp-action="SaveDraft">` (the form action tag helper) writes a `formaction` so one form can post to two actions. ## Errors and silent failures 1. A literal `href` together with any routing `asp-` attribute throws `InvalidOperationException` at render time; a plain `href` alone is left untouched. The form tag helper applies the same rule to a literal `action`. 2. `asp-route` combined with `asp-controller` or `asp-action` also throws `InvalidOperationException`, because the helper cannot tell which way to generate the URL. 3. When no route can produce a URL for the values (a misspelled action, a missing required parameter), nothing throws: the URL helper returns `null` and the link renders with an empty `href`. Seeing an empty `href` in the page source is the clue to look at route values. ## Compared with the HTML helpers `@Html.ActionLink("Care guide", "Details", "Plants", new { id = 7 })` produces the same `href` through the same URL generation, so the choice is about the markup, not the URL: - With the tag helper the element stays plain HTML: `class`, `data-*` and `aria-*` attributes are written as ordinary attributes instead of an anonymous `htmlAttributes` object. - The link text and any nested markup (an icon, a badge) sit inside the element instead of being passed as a string. - Editors and designers can read the view without knowing helper overloads. Teams usually standardise on tag helpers for new views and leave existing `Html.ActionLink` calls alone until the view is touched. ## Form specifics The form tag helper writes `action` from the same attributes, and when it generates the form it also writes `method="post"` unless the markup already sets `method`. It targets every `<form>` element, not just those with `asp-` attributes, which matters for the hidden request token it can add (that token is an antiforgery topic). A form that posts to the current URL needs no `asp-` attributes at all. How the URL helper chooses among routes, and `Url.Action` or `LinkGenerator` in code, belong to URL generation; the tag helper's job is turning attributes into route values and writing the result.
- Why does a link in an Admin-area view point into the area when you only wrote asp-controller and asp-action?URL generation reuses ambient route values from the current request, and `area` is one of them. A link rendered under `/Admin/...` keeps `area=Admin` unless the markup overrides it. Writing `asp-area=""` clears it so the link reaches a root controller; the MVC template's layout does exactly that on its navigation links.
- A link renders with an empty href and no exception. What happened?The anchor tag helper asked the URL helper for a URL and got `null`, because no route could be built from the values: a misspelled `asp-action`, an action on another controller without `asp-controller`, or a required route parameter with no `asp-route-*` value. The helper writes the empty result rather than throwing, so the page source is the place to spot it.
- asp-route-* values are always appended as query-string parameters
- Writing href and asp-action on one link lets the href win silently
- Omitting asp-controller links to HomeController by default
- An unknown action makes the anchor tag helper throw at render time
- asp-route can be combined with asp-action to pick a route and an action