Skip to content

Api versioning

Enzo Thomazi Lucas edited this page Jan 20, 2023 · 3 revisions

Custom API versioning configuration to match with the folder organization of the system.

Mandatory step

To use this feature of the library at full potential, you have to mark you controller with this attributes:

With route version defined:

[Route("api/v{version:apiVersion}/restaurants")]
public class RestaurantsController : ControllerBase
{
    //Some code...
}

Results:

api-with-version

The version controll is made using this dropdown:

api-with-version

Without route version defined

[Route("api/restaurants")] 
public class RestaurantsController : ControllerBase
{
    //Some code...
}

Results:

api-without-version

Description

With this feature, your api versioning can be visible on the organization of your code, to use it at full potential, you need only these two attributes:

[ApiVersion("1.0")] // This attribute defines the api version, the default of the library is one
[Route("api/v{version:apiVersion}/routes")]   // this type of route with the "v{version:apiVersion}" parameter makes the version
                                              // part of the route, so you don't need to pass as a parameter

Your code organization can use folders to match the api versioning, as examples shows:

Features +
         |_ 
         | V1  +
         |     |_
         |       Controllers +
         |                   |_ 
         |                     RoutesController.cs
         |
         |_
           V2  +
               |_
                 Controllers +
                             |_ 
                               RoutesController.cs
//Features/V1/Controllers/RoutesController.cs
[ApiVersion("1.0")] 
[Route("api/v{version:apiVersion}/routes")]   
[ApiController]
[Produces("application/json")]
public class RoutesController : BaseController
{
    //Some code...
}
//Features/V2/Controllers/RoutesController.cs
[ApiVersion("2.0")] 
[Route("api/v{version:apiVersion}/routes")]   
[ApiController]
[Produces("application/json")]
public class RoutesController : BaseController
{
    //Some code...
}

Individual use

To use only this feature, you need to use this method:

public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
{
    builder.Services.AddDevKitApiVersioningConfiguration();

    //Some code...
}
Clone this wiki locally