In ASP.NET Core MVC, while handling /Bookings/Details/42, what do Url.Action("Edit") and Url.Action("Details") return, and how do ambient values decide that?
answer
- ambient = current request's route values
- explicit = the values you pass
- a change invalidates values to its right
- Url.Action copies controller and action
- extra explicit values go to the query
basics
~20 sUrl.Action("Edit") returns /Bookings/Edit and Url.Action("Details") returns /Bookings/Details/42. Ambient values from the current request fill gaps, but changing a value invalidates every ambient value to its right in the template, so id is reused only when the action stays the same.
solid answer
~40 sAmbient values are the current request's `RouteValues`; explicit values are what you pass. With the conventional route `{controller=Home}/{action=Index}/{id?}`, URL generation walks left to right: an ambient value is kept while the explicit value is absent or equal, and once an explicit value differs, that ambient value and all to its right are dropped. `Url.Action("Edit")` changes `action`, so ambient `id = 42` is dropped and the result is `/Bookings/Edit`; `Url.Action("Details")` keeps `action`, so `id` survives and you get `/Bookings/Details/42`. `controller` is reused in both. Explicit values that match no parameter go to the query string, and trailing defaults collapse, so `Url.Action("Index", "Home")` is `/`. When a link comes back `null`, the docs' advice is to pass more values explicitly.
code
razor · 6 lines@* Rendered while handling /Bookings/Details/42 *@
<a href="@Url.Action("Details")">Refresh</a> @* /Bookings/Details/42 *@
<a href="@Url.Action("Edit")">Edit (id dropped)</a> @* /Bookings/Edit *@
<a href="@Url.Action("Edit", new { id = 42 })">Edit booking</a> @* /Bookings/Edit/42 *@
<a href="@Url.Action("Index", "Bikes", new { station = "central" })">Bikes</a>
@* /Bikes?station=central *@go deeper
Recall that Url.Action reuses the current controller when you pass only an action name.
Explain ambient versus explicit values and walk through the left-to-right invalidation rule for the default conventional route.
Diagnose null or surprising links from culture or area segments and MVC's special handling of action, controller and page, and fix them with explicit values or endpoint names.
Prefer explicit, name-based link generation in new code so URL output does not depend on whatever request happens to be rendering the link.
## Ambient and explicit values When MVC generates a URL during a request, it has two sources of route values: - **Ambient values**: `HttpContext.Request.RouteValues` of the current request, for `/Bookings/Details/42` under the default route that is `controller = Bookings`, `action = Details`, `id = 42`. - **Explicit values**: the values you pass to `Url.Action`, `Url.RouteUrl`, `LinkGenerator` and friends. Ambient values exist to save typing: linking to another action in the same controller should not require repeating the controller name. The difficulty is deciding which ambient values still make sense for the link you are building. ## Route value invalidation The docs call the rule **route value invalidation**. It assumes the URL is a hierarchy from left to right. For each parameter, in template order: 1. Ambient and explicit equal: keep going. 2. Ambient present, explicit absent: use the ambient value. 3. Explicit present and ambient absent, or the two differ: use the explicit value and **reject this and every later ambient value**. For controllers and Razor Pages, `controller`, `action`, `area` and `page` are **required values** of each endpoint, which is how the rule works for attribute routes whose templates do not mention them. ## Worked examples from /Bookings/Details/42 Template `{controller=Home}/{action=Index}/{id?}`; ambient `controller = Bookings, action = Details, id = 42`: | Call | Explicit values | Result | |---|---|---| | `Url.Action()` | action and controller copied from the request | `/Bookings/Details/42` | | `Url.Action("Details")` | action = Details (same) | `/Bookings/Details/42` | | `Url.Action("Edit")` | action = Edit (changed) | `/Bookings/Edit` | | `Url.Action("Edit", new { id = 42 })` | action, id | `/Bookings/Edit/42` | | `Url.Action("Index", "Bikes")` | controller changed | `/Bikes` | | `Url.Action("Index", "Bikes", new { station = "central" })` | plus a non-template value | `/Bikes?station=central` | | `Url.Action("Index", "Home")` | all defaults | `/` | Two extra rules are visible here. Explicit values that do not correspond to a template parameter become **query string** entries. And trailing parameters that equal their defaults are **collapsed**, which is why `/Bikes` is not `/Bikes/Index`. ## MVC's special cases The docs admit that "users are often surprised by the behavioral details of ambient values", because `IUrlHelper` adds rules of its own: - `Url.Action` always copies the current `action` and `controller` as **explicit** values unless you override them, which is why `Url.Action()` reproduces the current URL. - `Url.Page` copies the current `page` and resets `handler` to `null` unless overridden. - `LinkGenerator.GetPathByAction` and `GetPathByPage` with an `HttpContext` deliberately duplicate these anomalies for compatibility. `LinkGenerator.GetPathByName` without an `HttpContext` has no ambient values at all, which makes it the most predictable choice in new code. ## Attribute routes and required values Attribute-routed actions such as `[HttpGet("bookings/{id:int}/edit")]` have no `{controller}` or `{action}` parameters in their templates, yet `Url.Action("Edit", new { id = 42 })` still finds them. That works because MVC records `controller`, `action` and `area` as **required values** on each endpoint, and, per the docs, invalidation combines those required value names with the template's parameters and processes them left to right. In the common case that means an ambient `id` is reused only while the controller and action stay the same as the current request's. ## When the result is null The docs say calls that return `null` "are usually caused by not understanding route value invalidation" and recommend **specifying more of the route values explicitly**. A common case is a template with a value to the left of the one you change, such as `{culture}/{controller}/{action}/{id?}`: changing `culture` drops `id`, while `controller` and `action`, being required values, are kept. Another is linking from a page whose route has an `area` to a controller outside it: pass `area = ""` explicitly. ## What interviewers listen for - Ambient versus explicit values and why ambient values exist. - The left-to-right invalidation rule with a concrete result. - Extra values become query parameters; defaults collapse. - The fix for a `null` link: pass more values explicitly, or use endpoint names.
- With the template {culture}/{controller}/{action}/{id?} and the request /en/Bookings/Details/42, what does Url.RouteUrl with only culture = "de" keep?It keeps `controller` and `action`, because for controller endpoints they are required values that are never invalidated, and drops `id`, which sits to the right of the changed `culture`. The routing docs' version of this example produces the culture, controller and action with no id. To keep the id, pass it explicitly.
- How do explicit values that are not in the template appear in the generated URL?They are appended to the query string. From the docs' table: with ambient `controller = Home` and explicit `action = About, color = Red`, the result is `/Home/About?color=Red`. An ambient value that is not in the template, by contrast, is simply ignored.
It is like rebooking from your last rental form: the shop keeps the station you used, but if you change the station, the bike number you picked there is cleared, because it only made sense at the old station.
- Expects Url.Action("Edit") on /Bookings/Details/42 to produce /Bookings/Edit/42.
- Believes ambient values are always merged into every generated link.
- Thinks explicit values not in the template are silently dropped.
- Expects Url.Action("Index", "Bikes") to render /Bikes/Index rather than /Bikes.
- Responds to a null link by hardcoding the URL instead of passing missing values.