In ASP.NET Core request localization, in what order do the default culture providers run, and when does a provider's answer win?
answer
- query string, cookie, then Accept-Language
- culture and ui-culture query keys
- the .AspNetCore.Culture cookie
- only the first three Accept-Language values
- IRequestCultureFeature.Provider names the winner
basics
~10 sRequestCultureProviders runs QueryStringRequestCultureProvider, CookieRequestCultureProvider, then AcceptLanguageHeaderRequestCultureProvider. The first provider that returns a culture found in the supported lists, directly or through a parent, wins; if none does, DefaultRequestCulture applies.
solid answer
~40 s`RequestLocalizationOptions.RequestCultureProviders` starts with three providers in this order: `QueryStringRequestCultureProvider` (`?culture=` and `?ui-culture=`), `CookieRequestCultureProvider` (the `.AspNetCore.Culture` cookie) and `AcceptLanguageHeaderRequestCultureProvider`. The middleware asks them one at a time. A provider that has nothing returns `null` and the next one is asked. A provider that returns names none of which is supported, even after parent fallback, is logged at Debug and also skipped. The first provider with a supported culture or UI culture wins and the loop stops; if only one of the two matched, the other comes from `DefaultRequestCulture`. The header provider considers only the first three values by default (`MaximumAcceptLanguageHeaderValuesToTry`), ordered by quality. `IRequestCultureFeature.Provider` tells you which provider won.
code
http · 4 linesGET /guides/porto?culture=de HTTP/1.1
Host: travel.example
Cookie: .AspNetCore.Culture=c%3Dfr%7Cuic%3Dfr
Accept-Language: es-ES, en;q=0.8go deeper
Recall the three default providers in order, query string, cookie, Accept-Language, and that the default culture is used when none succeeds.
Explain first-supported-wins, the skip on unsupported names, parent fallback upward only, the three-value header limit and IRequestCultureFeature.Provider.
Choose and justify a provider order for a real product, decide whether the query string provider stays in production, and prove the behaviour in integration tests.
Weigh the trade-off between explicit choices, shared links and browser defaults across products and markets, and how the order interacts with caching and analytics.
## The default list | Order | Provider | Reads | Notes | |---|---|---|---| | 1 | `QueryStringRequestCultureProvider` | `?culture=` and `?ui-culture=` | keys set by `QueryStringKey` and `UIQueryStringKey`; one value fills both | | 2 | `CookieRequestCultureProvider` | cookie `.AspNetCore.Culture` | value `c=fr|uic=fr`; name set by `CookieName` | | 3 | `AcceptLanguageHeaderRequestCultureProvider` | `Accept-Language` | first `MaximumAcceptLanguageHeaderValuesToTry` values, default 3 | The list goes from the most explicit signal to the least: a link someone deliberately built, then a choice the visitor saved, then whatever the browser sends. The route-data provider is **not** in it; it has to be inserted. ## How the middleware evaluates them For each provider, in list order: 1. Call `DetermineProviderCultureResult(HttpContext)`. A `null` result means "nothing here" and the loop moves on. 2. Try each returned culture name against `SupportedCultures`, and each UI culture name against `SupportedUICultures`, comparing names case-insensitively. 3. If a name is not listed and parent fallback is on (the default), try its parent, `fr-CA` then `fr`, up to five levels. 4. If **neither** the culture nor the UI culture matched, log the rejected names at Debug and move to the next provider. 5. If **one** matched, take the other from `DefaultRequestCulture`. 6. Record the result and the winning provider, and stop. Two consequences surprise people. A provider with an unsupported answer does not end the search: `?culture=xx` is skipped and the cookie still gets its turn. And fallback goes only **up** the hierarchy: a request for `fr` does not match a list that contains only `fr-FR`. ## Accept-Language details The header provider takes the first three values **in header order**, then sorts those three by quality, highest first. With `Accept-Language: es-ES, pt-BR;q=0.9, fr;q=0.8, de;q=0.7` and a site supporting `fr` and `de`, it tries `es-ES`, `pt-BR` and `fr`, and `fr` wins. Had `fr` been fourth, nothing would match and the default would apply. Setting `MaximumAcceptLanguageHeaderValuesToTry` to `0` removes the limit; the limit exists so a long header cannot make the server parse many culture names. ## Seeing who won - `HttpContext.Features.Get<IRequestCultureFeature>()` returns the `RequestCulture` and the `Provider` that produced it, or `null` for the default. - `ApplyCurrentCultureToResponseHeaders = true` makes the middleware write `Content-Language` with the UI culture name, visible in any browser's network tools. - `Debug` logs under `Microsoft.AspNetCore.Localization` name each provider whose answer was rejected. ## A worked request The HTTP example sends three signals: `?culture=de`, a cookie saying `fr`, and `Accept-Language: es-ES, en;q=0.8`. With `en`, `fr` and `de` supported: 1. The query string provider returns `de` for both halves; `de` is supported; it wins. 2. The cookie and header providers are never called. 3. Remove `?culture=de` and the cookie provider returns `fr`, which wins. 4. Remove the cookie as well and the header provider tries `es-ES`, then `en`; `es-ES` and its parent `es` fail, `en` matches. 5. Remove the header too and every provider returns `null`; `DefaultRequestCulture` applies and `Provider` is `null`. ## Changing the order `RequestCultureProviders` is a plain `IList<IRequestCultureProvider>`: - `RemoveAt(0)` drops the query string provider when shared links must not override a saved choice; - `Insert(0, provider)` or the `AddInitialRequestCultureProvider` extension puts a provider first; - `CustomRequestCultureProvider` wraps a delegate, for example one that reads a signed-in user's stored language. ## Writing a provider - `CustomRequestCultureProvider` takes a `Func<HttpContext, Task<ProviderCultureResult?>>`; return `null` to mean "no opinion". - A class deriving from `RequestCultureProvider` overrides `DetermineProviderCultureResult` and can return the inherited `NullProviderCultureResult`. - Either way, the answer is still checked against the supported lists; a provider cannot force an unsupported culture. ## Mistakes interviewers listen for - putting `Accept-Language` ahead of the cookie in their description of the defaults; - believing the answers of several providers are merged; - believing an unsupported value stops the search; - expecting `fr` to match `fr-FR`.
- A request sends Accept-Language: es-ES, pt-BR;q=0.9, fr;q=0.8, de;q=0.7 and the site supports fr and de. Which culture applies, and what if fr were listed fourth?`fr`. The header provider takes the first three values, `es-ES`, `pt-BR` and `fr`, orders them by quality and tries each; only `fr` is supported. If `fr` were fourth, the provider would try only unsupported names, be skipped, and the default culture would apply unless `MaximumAcceptLanguageHeaderValuesToTry` were raised or set to 0.
- How does a test prove which provider set the culture?Read `IRequestCultureFeature` from `HttpContext.Features`: its `Provider` is the winning provider instance, or `null` when `DefaultRequestCulture` was used. In an integration test, an endpoint can echo the provider's type name and the chosen culture, and requests can vary the query string, cookie and header one at a time.
- Accept-Language is checked before the culture cookie by default.
- The middleware merges the answers of all providers into one culture.
- A provider returning an unsupported culture stops the search and forces the default.
- Every value in a long Accept-Language header is tried in turn.
- With parent fallback on, a request for fr matches a supported fr-FR.