Take advantage of API versioning in ASP.NET Core to maintain multiple versions of the same API. Credit: Carlos Castilla / Getty Images API versioning in a Web API lets you preserve multiple versions of the same API while at the same time keep the same URI as much as possible. With ASP.NET Core, API versioning has been made much easier. One objective of Web API is to serve multiple clients, whether based on homogenous or heterogenous platforms. Because you might have several clients using an API, upgrading an API while at the same time maintaining support for the previous versions is a challenge. You might have a few users who would like to use the old API. That’s exactly where API versioning fits in. When building RESTful services, it is imperative that you have a versioning strategy in place so that you can maintain multiple versions of the API. You should ensure that genuine calls to your API doesn’t get rejected due to the versioning policy that you have defined. You should take a look at the versioning semantics outlined in the Microsoft REST API guidelines. Create an ASP.NET Core API project First, let’s create an ASP.NET Core project in Visual Studio. With Visual Studio 2017 or 2019 is installed in your system, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio: Launch the Visual Studio IDE. Click Create New Project. In the Create New Project window, select ASP.NET Core Web Application from the list of the templates. Click Next. In the Configure Your New Project window that appears, specify the name and location for the new project. Click Create. A new window, Create New ASP.NET Core Web Application, appears. Select .NET Core as the runtime and ASP.NET Core 2.2 (or later) from the dropdown list at the top. Select API as the project template to create a new ASP.NET Core API application. Ensure that the check boxes Enable Docker Support and Configure for HTTPS are unchecked, because you won’t be using those features here. Ensure that Authentication is set to No Authentication, because you won’t be using authentication either. Click Create. This creates a new ASP.NET Core API project in Visual Studio. Install the NuGet package Now that you have created an ASP.NET Core API project in Visual Studio, install the necessary NuGet Package. To do this, select the project in the Solution Explorer Window, right-click it, and then select Manage NuGet Packages in the contextual menu that appears. Then install the following package from the NuGet Package Manager: Microsoft.AspNetCore.Mvc.Versioning. Alternatively, you can install the package from the NuGet Package Manager Console by typing the following command. Install-Package Microsoft.AspNetCore.Mvc.Versioning Enable API versioning So far so good. To enable API versioning, add a call to the AddApiVersioning method in the ConfigureServices method. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddApiVersioning(); } You need the following namespace to be included: Microsoft.AspNetCore.Mvc.Versioning. You can specify default versioning as well: public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddApiVersioning(v => { v.ReportApiVersions = true; v.AssumeDefaultVersionWhenUnspecified = true; v.DefaultApiVersion = new ApiVersion(1, 0); }); } Versioning using QueryString Parameter Consider the following API: [ApiVersion("2.0")] [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2" }; } //API methods } You can access this API using this URL: https://localhost:51868/api/values?api-version=2.0. Replace the port number with the port number at which this API runs using IIS Express in your system. Versioning using routes In route-based versioning, you should specify the version of the API in the URL. consider the following API. Note how the route has been specified to include versioning. [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult<string> Get() { return "Hello World"; } } Next, you can access the Get method of this API using the following URL: https://localhost:51868/api/v1.0/values. Ignore API versioning You might have some APIs that need just one version. For such APIs, you might want to ignore API versioning using the [ApiVersionNeutral] attribute as shown here: [ApiVersionNeutral] [Route("api/[controller]")] [ApiController] public class HomeController : ControllerBase { [HttpGet] public ActionResult<string> Get() { return "ApiVersionNeutral attribute demo."; } } Related content news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news AI accelerating API development, IBM says Generative AI is helping API teams complete projects faster, while APIs also are fueling the use of AI, company official says. By Paul Krill Nov 07, 2024 2 mins Generative AI APIs Artificial Intelligence news WSO2 API managers manage AI APIs WSO2 API Manager and WSO2 API Platform for Kubernetes now allow developers to manage AI services as APIs, with support for OpenAI, Mistral A,I and Microsoft Azure OpenAI. By Paul Krill Nov 05, 2024 3 mins Generative AI APIs Devops Resources Videos