ASP.NET Core 6 introduces a simplified internet hosting mannequin that enables us to construct light-weight APIs with minimal dependencies. Now we have mentioned getting began with minimal APIs, utilizing logging and dependency injection with minimal APIs, and testing minimal APIs in earlier article right here. On this article, we’ll study how we are able to implement versioning for our minimal API endpoints.
We’ll illustrate how we are able to model minimal APIs in ASP.NET 6 Core by following this sequence of six steps:
- Create a ASP.NET 6 Core Minimal API challenge in Visible Studio 2022
- Set up the mandatory NuGet package deal(s)
- Add API versioning assist to our challenge
- Create a model set
- Create API endpoints and affiliate the model set
- Execute the API endpoints in Postman
To work with the code examples supplied on this article, it is best to have Visible Studio 2022 put in in your system. For those who don’t have already got a replica, you possibly can obtain Visible Studio 2022 right here.
Create an ASP.NET Core 6 minimal Net API challenge in Visible Studio 2022
First off, let’s create an ASP.NET Core 6 challenge in Visible Studio. Following these steps will create a brand new ASP.NET Core 6 Net API challenge in Visible Studio 2022:
- Launch the Visible Studio 2022 IDE.
- Click on on “Create new challenge.”
- Within the “Create new challenge” window, choose “ASP.NET Core Net API” from the record of templates displayed.
- Click on Subsequent.
- Within the “Configure your new challenge” window, specify the identify and placement for the brand new challenge.
- Optionally test the “Place resolution and challenge in the identical listing” test field, relying in your preferences.
- Click on Subsequent.
- Within the “Further Info” window proven subsequent, uncheck the test field that claims “Use controllers…” since we’ll be utilizing minimal APIs on this instance. Depart the “Authentication Sort” as “None” (default).
- Make sure that the test containers “Allow Docker,” “Configure for HTTPS,” and “Allow Open API Assist” are unchecked as we gained’t be utilizing any of these options right here.
- Click on Create.
We’ll use this ASP.NET Core 6 Net API challenge to create minimal API endpoints and implement API versioning within the sections beneath.
Set up the API versioning NuGet packages
ASP.NET Core 6 minimal APIs assist versioning utilizing any of those three packages:
- Asp.Versioning.Http — used to offer assist for versioning in minimal APIs.
- Asp.Versioning.Mvc — used to offer assist for versioning in MVC Core apps.
- Asp.Versioning.Mvc.ApiExplorer — used to offer assist within the API explorer extensions for ASP.NET Core API versioning
In case you have efficiently created an ASP.NET Core 6 net software challenge in Visible Studio 2022, the following factor it is best to do is add the mandatory NuGet packages to your challenge. To do that, choose the challenge within the Answer Explorer window, right-click, and choose “Handle NuGet Packages….” Within the NuGet Package deal Supervisor window, seek for the next package deal and set up it.
Asp.Versioning.Http
Alternatively, you possibly can set up the package deal by way of the NuGet Package deal Supervisor Console as proven beneath.
PM> Set up-Package deal Asp.Versioning.Http
Add API versioning assist to the providers assortment in ASP.NET Core 6
So as to add API versioning assist within the providers assortment for the minimal API, it is best to write the next code within the Program.cs file:
builder.Companies.AddApiVersioning(choices =>
{
choices.DefaultApiVersion = new ApiVersion(1, 0);
choices.ReportApiVersions = true;
choices.AssumeDefaultVersionWhenUnspecified = true;
choices.ApiVersionReader = new HeaderApiVersionReader("api-version");
});
Word how the default API model has been specified. The ApiVersionReader property is used to specify the important thing that shall be utilized by the shopper to cross the API model when calling the API endpoints. When the worth of the AssumeDefaultVersionWhenUnspecified property is true, the default API model shall be used if the shopper has not specified an API model.
Word that you would be able to mix HeaderApiVersionReader and QueryStringApiVersionReader to allow purchasers to specify model data in a number of methods when calling the API endpoints.
providers.AddApiVersioning(choices =>
{
choices.DefaultApiVersion = new ApiVersion(1, 0);
choices.AssumeDefaultVersionWhenUnspecified = true;
choices.ReportApiVersions = true;
choices.ApiVersionReader =
ApiVersionReader.Mix(
new HeaderApiVersionReader("Api-Model"),
new QueryStringApiVersionReader("Question-String-Model"));
});
Add API variations utilizing a model set in ASP.NET Core 6
Now outline a brand new model set in your API utilizing the next code.
var versionSet = app.NewApiVersionSet()
.HasApiVersion(1.0)
.HasApiVersion(2.0)
.ReportApiVersions()
.Construct();
We’ll use this model set within the subsequent part to create our API endpoints.
Create minimal API endpoints in ASP.NET Core 6
We’ll create two endpoints right here to maintain it easy and deal with versioning our minimal APIs. Write the next code within the Program.cs file to create two endpoints.
app.MapGet("/GetMessage", () => "That is an instance of a minimal API").WithApiVersionSet(versionSet).MapToApiVersion(1.0);
app.MapGet("/GetText", () => "That is one more instance of a minimal API").WithApiVersionSet(versionSet).WithApiVersionSet(versionSet)
.IsApiVersionNeutral();
Word how we have now related the model set that we created within the earlier part. The MapToApiVersion technique maps a specific endpoint to a selected model. The IsApiVersionNeutral technique marks an endpoint as impartial to API versioning.
Right here is the entire supply code of the Program.cs file in your reference:
utilizing Asp.Versioning;
utilizing Asp.Versioning.Conventions;
var builder = WebApplication.CreateBuilder(args);
// Add providers to the container.
builder.Companies.AddApiVersioning(choices =>
{
choices.DefaultApiVersion = new ApiVersion(1, 0);
choices.ReportApiVersions = true;
choices.AssumeDefaultVersionWhenUnspecified = true;
choices.ApiVersionReader = new HeaderApiVersionReader("api-version");
});
var app = builder.Construct();
var versionSet = app.NewApiVersionSet()
.HasApiVersion(1.0)
.HasApiVersion(2.0)
.ReportApiVersions()
.Construct();
// Configure the HTTP request pipeline.
app.MapGet("/GetMessage", () => "That is an instance of a minimal API").WithApiVersionSet(versionSet).HasApiVersion(new ApiVersion(2, 0));
app.MapGet("/GetText", () => "That is one other instance of a minimal API").WithApiVersionSet(versionSet).IsApiVersionNeutral();
app.Run();
Execute the minimal API endpoints
Now press the F5 key in Visible Studio 2022 IDE to run the applying. The next screenshot (Determine 1) exhibits the error you’ll encounter if you happen to name the /getmessage endpoint with out specifying the API model key within the request header.
The subsequent screenshot (Determine 2) exhibits how the output will look once you specify the API model key within the request header and name the /getmessage endpoint once more.
As a result of the /gettext endpoint is marked as API model impartial, you don’t must specify an API model when calling this endpoint. While you execute the /gettext endpoint, the response will seem like that proven within the screenshot (Determine 3) beneath.
This being a minimal implementation, we have now not used a knowledge context, a database, a repository class, and even any mannequin lessons. Now we have merely outlined two easy API endpoints for instance how we are able to model minimal APIs in ASP.NET Core 6.
Copyright © 2022 IDG Communications, Inc.