API Versioning in Minimal API.
Tags: aspdotnet, dotnet, csharp, learning
Published: 2024-06-02 07:18:52

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:
- [JSON Placeholder](https://jsonplaceholder.typicode.com)
- [Platzi](https://fakeapi.platzi.com/)

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 😁.
```bash
dotnet new web -o MinimalApi -f .net8.0
```
`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75a7lh80o3dm8ca8nwbg.png)

Install packages
```bash
dotnet add package Asp.Versioning.Http
```
```bash
dotnet add package Asp.Versioning.Mvc.ApiExplorer
```
Now create a service folder inside your project folder and add a file `ApiVersionExtension.cs`, your file look like this:

![Extension method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3la1jtlesq1uzgzl5yfn.png)

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
```CSharp
config.ApiVersionReader = ApiVersionReader.Combine(
                new QueryStringApiVersionReader("api-version"),
                new HeaderApiVersionReader("api-x-version")
            );
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b9ui5ytrrf6goy2a86m.png)

In variable `apiVersionSet` I've provided 2nd version as current and 1st version as deprecated.
```bash
dotnet run
```
OR
```bash
dotnet watch
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ugzkgn1e8r011ff0w62.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o39wyjtk6otpi8pjymp1.png)

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.