How would you version a public ASP.NET Core API without breaking existing web and mobile clients?
PhonePe · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, ASP.NET Core, Software Engineering
Community answers
Posted by wasAsked editorial collection
Prefer compatible evolution within a version: add optional fields, tolerate unknown fields, and never silently change meaning. Introduce a new version only for a real breaking contract change.
URL segments (/v2/orders) are explicit and cache-friendly; headers keep URLs stable but are less visible. Pick one policy and document it with OpenAPI. Keep versions side-by-side during migration, publish deprecation and sunset dates, measure client usage, and provide a migration guide. Contract tests should protect old consumers. Versioning does not replace staged database/message compatibility, and it should not encode ordinary product releases or implementation details.
Backbencher Developer · · 0 likes
Versioning protects existing clients — especially mobile apps that users do not update — while the API evolves.
First, avoid unnecessary versions
- Make additive, backward-compatible changes: new optional fields, new endpoints, new enum values only if clients tolerate unknown values.
- Clients should ignore unknown fields, and the server should accept missing optional fields.
- Breaking changes (removing or renaming fields, changing types or meanings, stricter validation) need a new version.
Choosing a scheme
- URL path (/api/v1/orders): visible, cache-friendly, easy for mobile and third parties. The most common choice for public APIs.
- Query string (?api-version=2024-10-01): used by Azure-style APIs with date-based versions.
- Header or media type (api-version header, Accept: application/vnd.company.v2+json): clean URLs but harder to test and cache.
Implementation in ASP.NET Core with Asp.Versioning:
builder.Services.AddApiVersioning(o =>
{
o.DefaultApiVersion = new ApiVersion(1, 0);
o.AssumeDefaultVersionWhenUnspecified = true;
o.ReportApiVersions = true; // api-supported-versions header
}).AddApiExplorer(o => o.GroupNameFormat = "'v'VVV");
Map controllers or minimal API groups to versions, and generate one OpenAPI document per version.
Keep versions maintainable
- Share business logic; keep version differences in the request and response mapping layer, not duplicated services.
- Document changes in a changelog.
Deprecation policy
- Announce deprecation with response headers (Deprecation, Sunset, a Link to the docs) and in documentation.
- Monitor usage per version and per client (app version header) to know when it is safe to remove.
- For mobile apps, also implement a minimum-supported-version check that prompts users to update.
- Give a clear timeline (for example 6–12 months) before removing a version.
Test both versions in CI with contract tests so a change for v2 does not break v1.