DEV Community

Cover image for API Versioning in Minimal API.
Ayush
Ayush

Posted on

API Versioning in Minimal API.

Hello guys, Sorry I took so so so soooooooooo, long break, I came back with this topic, I know I haven't covered properly my previous posts, but I'll continue again. Apart from this, let's continue this topic, as you seen in the title, Its API versioning in Minimal API, but in ASP.NET Core 8, yes you can do that with .NET 6 or higher version.

What is API Versioning?

As you've seen in the fake API sites, which provide the fake API for your building and learning skills of Web Apps, or other Platforms, If you don't know about the Fake APIs, then visit the given links down below:

As you can see their given fake API URL segment have /v1 in the Platzi API, that's indicating the URLSegmentVersioning which can be access by passing the v1 to define, that its using the API version 1.0 and you can define which API endpoints can use as v1 or v2 or depricated. If you don't get my point, then you can search more on internet.

Prerequisite

  • .NET 8 SDK.
  • Visual Studio Code
  • Knowledge about the extension method.
  • Asp.Versioning.Http
  • Asp.Versioning.Mvc.ApiExplorer

You can start with default template of dotnet and start creating your first, Minimal API, maybe you know already because you're smarter, but still, I'll do the developer thing Copy and Paste šŸ˜.



dotnet new web -o MinimalApi -f .net8.0


Enter fullscreen mode Exit fullscreen mode

Tadow Your Minimall API template is created, open that folder into your Visual Studio Code, you code will look like this simple, example given below:

Initial code

Install packages



dotnet add package Asp.Versioning.Http


Enter fullscreen mode Exit fullscreen mode


dotnet add package Asp.Versioning.Mvc.ApiExplorer


Enter fullscreen mode Exit fullscreen mode

Now create a service folder inside your project folder and add a file ApiVersionExtension.cs, your file look like this:

Extension method

Now use this extension method into you Program.cs file, so that it can be defined globally, as you can see in the extension method, there's line



config.ApiVersionReader = ApiVersionReader.Combine(
                new QueryStringApiVersionReader("api-version"),
                new HeaderApiVersionReader("api-x-version")
            );


Enter fullscreen mode Exit fullscreen mode

This class QueryStringApiVersionReader defines that you can give your API version as /endpoint?api-version=1 and same goes to the second class HeaderApiVersionReader which defines that you can pass the api-x-version property to Header of API and give the version value, to the endpoint.
Next and final step, to define the API endpoints and give the api-endpoints version.

Program.cs

In variable apiVersionSet I've provided 2nd version as current and 1st version as deprecated.



dotnet run


Enter fullscreen mode Exit fullscreen mode

OR



dotnet watch


Enter fullscreen mode Exit fullscreen mode

You can run one of those commands to run your API, from first of all you'll see your browser as this:

Run on a port

Now change the URL on the search bar to http://localhost:5202?api-version=2 and your API executed and display the text message.

Result

As you can see in the Header, the deprecated and supported API version. I hope this post is helpful for you and now you can create your own API versions.

Top comments (0)