ASP.NET Core API versioning

With API’s often changing over time you may want to plan for versioning early on. ASP.NET Core has a library that makes versioning API a bit easier. By tagging controllers and actions with version number one can run multiple versions simultaneously.

Install

Implement

In Startup.cs ConfigureServices:

If you want to keep some URL’s without versioning, or have an existing API that is already in use then you can configure default versioning by adding:

Use

Versions can be in format:

  • [Version Group.]<Major>.<Minor>[-Status]
  • <Version Group>[<Major>[.Minor]][-Status]

Examples:

  • /api/foo?api-version=1.0
  • /api/foo?api-version=2.0-Alpha
  • /api/foo?api-version=2015-05-01.3.0
  • /api/v1/foo
  • /api/v2.0-Alpha/foo
  • /api/v2015-05-01.3.0/foo

Tag your controller with [ApiVersion()]. Note that you can use the tags multiple times if a controller has multiple versions. You can also make a service version neutral by tagging it with [ApiVersionNeutral].

Since we are using api area we also add version string to [Route] between api and controller name. But this would notwork with “default version” as far as I can see.

You can map individual methods within the controller, here using the example of “Dual”:

1 thought on “ASP.NET Core API versioning”

  1. translated by Google.
    Dear, I followed the tutorial and it works locally, however, when I upload it to the server and conserve the service “version 2.0”, it returns the data of the “version 1.0”.
    I need to configure something else?

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Tedds blog

Subscribe now to keep reading and get access to the full archive.

Continue reading