0

I am using ASP.NET ZERO (Angular and ASP.Net Core template). How to create different API versions, I want to support different version for backward compatibility. For example /api/services/app/Customer/GetAll - this is current version /api/services/app/v2/Customer/GetAll - this is next version I want to create.And want to support both these versions. Can someone please guide me through this?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
devson
  • 990
  • 8
  • 22

2 Answers2

1

You can implement API versioning in many ways.

  1. URI's
  2. Query String
  3. Version Header
  4. Accept Header
  5. Media Type

To answer your question on by Uri, below is the sample example.

Define Routes

config.Routes.MapHttpRoute(
    name: "Version1",
    routeTemplate: "api/v1/Students/{id}",
    defaults: new { id = RouteParameter.Optional, controller = "StudentsV1" }
);

config.Routes.MapHttpRoute(
    name: "Version2",
    routeTemplate: "api/v2/Students/{id}",
    defaults: new { id = RouteParameter.Optional, controller = "StudentsV2" }
);

Use Route Attribute

public class StudentsV1Controller : Controller
{
    [Route("api/v1/students")]
    public IEnumerable<StudentV1> Get() {...}

    [Route("api/v1/students/{id}")]
    public StudentV1 Get(int id) {...}
}

public class StudentsV2Controller : Controller
{
    [Route("api/v2/students")]
    public IEnumerable<StudentV2> Get() {...}

    [Route("api/v2/students/{id}")]
    public StudentV2 Get(int id) {...}
}

Reference: https://csharp-video-tutorials.blogspot.com/2017/02/web-api-versioning-using-uri.html

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

ASP.NET API Versioning is meant to solve this exact problem using any method you choose, including all of the methods listed above. There is extensive documentation and examples for various configurations. You can drop in API Versioning with little-to-no changes in your existing application. There is also support for OpenAPI/Swagger support if you need it.

The template is just scaffolding. You can use API Versioning regardless of how you initially started or created your project.

Despite it's popularity, I advise against versioning by URL segment. If you care about adhering to the REST constraints, it's the least RESTful method (it violates the URLs as an identifier under the Uniform Interface constraint). If you have the option, I would consider other methods. While not the most RESTful, the query string method is typically the most pragmatic in ease of use without violating any constraints.

I'm happy to elaborate further or answer questions if you have them.

Chris Martinez
  • 3,185
  • 12
  • 28