web-api
  1. web-api-versioning-using-uri

Versioning Using URI - (Web API Versioning)

API versioning is an important concept when building web APIs. Versioning allows developers to make changes to their API while avoiding breaking changes for existing clients. In this page, we'll discuss how to use URI versioning in Web API versioning.

Syntax

To version a Web API using URI versioning, add the version number to the URI. Here is an example URI:

https://api.example.com/v1/customers

Example

Here's an example of how to use URI versioning in a Web API:

using Microsoft.AspNetCore.Mvc;

namespace MyNamespace.Controllers
{
  [ApiController]
  [Route("v1/[controller]")]
  public class CustomersController : ControllerBase
  {
    [HttpGet("{id}")]
    public IActionResult Get(int id)
    {
      // Code to get a customer by ID
    }
  }
}

Output

When a client requests /v1/customers/1, they receive data for customer #1. As the API changes over time, the URI can be updated to /v2/customers/1 to indicate a new version of the API.

Explanation

In the example code, we define a CustomersController that handles HTTP GET requests with an ID parameter. The [Route("v1/[controller]")] attribute specifies that this controller should be accessed with the /v1/customers endpoint. If the API changes, a new version can be created by adding a new controller with a different route, such as [Route("v2/[controller]")].

Use

URI versioning is a simple and easy-to-understand way to version your Web API. It allows clients to easily find the version they need and make calls to the correct endpoint.

Important Points

  • URIs can be used to version a Web API by adding the version number to the URI.
  • Changes to the API can result in new versions with new URIs.

Summary

In this page, we've discussed how to use URI versioning in Web API versioning. We covered the syntax, example, output, explanation, use, important points, and summary of versioning a Web API using URIs. URI versioning is a simple and effective way to version a Web API, as it allows clients to easily find the version they need and make calls to the correct endpoint.

Published on: