skip to content

When moving loyalty-points clients from v1 to v2 with Asp.Versioning 10, what do AssumeDefaultVersionWhenUnspecified, the version selector, ReportApiVersions and sunset policies each do?

level: middleimportance: should knowfreq 40%

answer

  1. AssumeDefaultVersionWhenUnspecified defaults to false
  2. DefaultApiVersionSelector picks DefaultApiVersion
  3. CurrentImplementationApiVersionSelector floats to newest
  4. api-supported-versions, api-deprecated-versions
  5. options.Policies.Sunset(1.0).Effective(...)

basics

~10 s

AssumeDefaultVersionWhenUnspecified lets unversioned requests through by asking the ApiVersionSelector for a version, 1.0 with the default selector. ReportApiVersions adds api-supported-versions and api-deprecated-versions headers, and options.Policies.Sunset or Deprecate adds dated policy headers for a version.

solid answer

~40 s

`AssumeDefaultVersionWhenUnspecified = true` turns a missing version from a 400 into a chosen version: the `ApiVersionSelector` picks it. The default `DefaultApiVersionSelector` always returns `DefaultApiVersion` (1.0), which keeps legacy clients pinned; `CurrentImplementationApiVersionSelector` returns the newest implemented version without a status, which silently moves them to v2 on the day v2 ships. It cannot help URL-segment routes, whose template requires the segment. `ReportApiVersions = true` (or `.ReportApiVersions()` on one API) adds `api-supported-versions` and `api-deprecated-versions` to responses, and versions declared deprecated move to the second header. `options.Policies.Sunset(1.0).Effective(date).Link("...")` and `options.Policies.Deprecate(1.0)` attach dated policies, which the reporter writes as `Sunset`, `Deprecation` and `Link` headers. None of them stops serving v1; retiring it means removing its endpoints.

code

csharp · 14 lines
csharp
builder.Services.AddApiVersioning(options =>
{
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.AssumeDefaultVersionWhenUnspecified = true;   // kiosk sends no version
    // ApiVersionSelector stays DefaultApiVersionSelector: unversioned = 1.0
    options.ReportApiVersions = true;

    options.Policies.Deprecate(1.0)
        .Effective(DateTimeOffset.UtcNow)
        .Link("https://loyalty.example/versions");
    options.Policies.Sunset(1.0)
        .Effective(new DateTimeOffset(2027, 3, 31, 0, 0, 0, TimeSpan.Zero))
        .Link("https://loyalty.example/versions");
});

go deeper

for a junior

Recall that a missing version is a 400 unless AssumeDefaultVersionWhenUnspecified is turned on, and that ReportApiVersions is off by default.

for a middle

Explain how the selector picks the assumed version, which headers reporting adds, and why assuming a default does nothing for URL-segment routes.

for a senior

Plan a v1 to v2 migration that cannot surprise unversioned clients: pinned selector, deprecation and sunset policies, reporting, and a removal step.

for a principal

Weigh how long legacy unversioned access is tolerated and how deprecation dates are communicated and enforced across teams.

## The migration problem The loyalty-points API has v1, used by an old kiosk app that never sent a version, and v2 with tiers and expiring points. You want v2 adopted, the kiosk not broken, and v1 eventually retired. Asp.Versioning gives four knobs; it is important to know what each does **not** do. ## AssumeDefaultVersionWhenUnspecified and selectors With `AssumeDefaultVersionWhenUnspecified = false`, the default, a request without a version to a versioned endpoint gets 400 `ApiVersionUnspecified`. Setting it to `true` makes the matcher ask the configured **`IApiVersionSelector`** for a version instead: | Selector | Picks | Risk | |---|---|---| | `DefaultApiVersionSelector` (default) | `DefaultApiVersion`, 1.0 unless changed | none for old clients; they stay on 1.0 | | `CurrentImplementationApiVersionSelector` | newest implemented version without a status, else the default | unversioned clients jump to v2 when it ships | | `LowestImplementedApiVersionSelector` | the lowest implemented version without a status | old clients stay on the oldest version that still exists | | `ConstantApiVersionSelector` | one fixed version you pass | must be updated deliberately | The library's analyzer **AV0016** ("Do not assume default API version") reports the setting where it judges it unnecessary, a reminder that it exists to grandfather clients that predate versioning. For the kiosk, the safe pairing is `AssumeDefaultVersionWhenUnspecified = true` with the default selector and `DefaultApiVersion = new ApiVersion(1, 0)`. A floating selector is how a release turns into a breaking change for clients that never asked for one. One limit: assuming a version happens during version matching, after routing. A route whose template contains `v{version:apiVersion}` never matches a URL without the segment, so assuming a default helps only query-string, header or media-type versioning. ## ReportApiVersions `options.ReportApiVersions = true`, or `.ReportApiVersions()` on a version set or API, adds two response headers: - `api-supported-versions`: versions implemented and not deprecated, such as `2.0`. - `api-deprecated-versions`: versions still served but deprecated, such as `1.0` after `[ApiVersion(1.0, Deprecated = true)]` or `HasDeprecatedApiVersion(1.0)`. The headers also appear on the unspecified, invalid and unsupported client-error responses, so a client that sent a bad version learns what exists. ## Sunset and deprecation policies Policies attach dates and documentation links to a version: 1. `options.Policies.Deprecate(1.0).Effective(DateTimeOffset.UtcNow).Link("https://loyalty.example/versions")` 2. `options.Policies.Sunset(1.0).Effective(new DateTimeOffset(2027, 3, 31, 0, 0, 0, TimeSpan.Zero)).Link("https://loyalty.example/versions")` When versions are reported, the reporter writes a `Sunset` header with the date, a `Deprecation` header (only when the deprecation takes effect before any sunset date) and `Link` headers pointing at the policy pages. What those headers mean to a client, and how a deprecation programme runs, is HTTP protocol design rather than this library. ## What none of these do - Deprecating or sunsetting v1 does not reject v1 requests. After the sunset date the library keeps serving them until you remove the v1 endpoints. - Reporting does not redirect anybody to v2. - Assuming a default does not record which clients relied on it; log `HttpContext.RequestedApiVersion` together with whether the request carried a version if you need to find them. ## A sequence that works 1. Ship v2 alongside v1, with `ReportApiVersions` on. 2. Mark 1.0 deprecated and add a sunset policy with a link. 3. Keep unversioned clients on 1.0 with the default selector while they are contacted. 4. Remove the v1 endpoints after the sunset date; requests for 1.0 then get `UnsupportedApiVersion` (a 404 if the version is in the URL path).

  • Why is CurrentImplementationApiVersionSelector risky with AssumeDefaultVersionWhenUnspecified?
    It selects the newest implemented version without a status. Unversioned clients were on 1.0 yesterday and are on 2.0 the moment 2.0 is deployed, with no change on their side, so any breaking difference in 2.0 hits them. The default selector pins them to `DefaultApiVersion` instead.
  • Does a sunset policy make Asp.Versioning return an error after the date?
    No. The policy only produces `Sunset` and `Link` headers when versions are reported. Requests for 1.0 keep being served until you delete the 1.0 endpoints; only then do they get the `UnsupportedApiVersion` client error, or a 404 when the version is in the URL path.

  • AssumeDefaultVersionWhenUnspecified always routes unversioned requests to the newest version.
  • With AssumeDefaultVersionWhenUnspecified, /api/points works for a v{version:apiVersion} route.
  • ReportApiVersions is on by default, so clients already see supported versions.
  • A sunset policy makes the library reject the version after its effective date.
  • Deprecated versions disappear from responses; clients get 400 for them.

Assumes Asp.Versioning 10.2: option defaults, the four selectors, the reporter's header names and the Policies builder are read from that release.