nearly Easy methods to model minimal APIs in ASP.NET Core 6 will cowl the most recent and most present data re the world. edit slowly consequently you perceive skillfully and accurately. will buildup your information cleverly and reliably
ASP.NET Core 6 introduces a simplified internet hosting mannequin that permits us to create light-weight APIs with minimal dependencies. We have mentioned getting began with minimal APIs, utilizing logging and dependency injection with minimal APIs, and testing minimal APIs within the earlier article right here. On this article, we’ll study how we will implement versioning for our minimal API endpoints.
We’ll illustrate how we will model minimal APIs in ASP.NET 6 Core by following this six-step sequence:
- Create an ASP.NET 6 Core Minimal API mission in Visible Studio 2022
- Set up the required NuGet packages
- Add API model assist to our mission
- Create a model set
- Create API endpoints and affiliate model set
- Run API endpoints in Postman
To work with the code samples supplied on this article, you will need to have Visible Studio 2022 put in in your system. When you do not have already got a duplicate, you’ll be able to obtain Visible Studio 2022 right here.
Create a minimal ASP.NET Core 6 Net API mission in Visible Studio 2022
First, let’s create an ASP.NET Core 6 mission in Visible Studio. Following these steps will create a brand new ASP.NET Core 6 Net API mission in Visible Studio 2022:
- Begin the Visible Studio 2022 IDE.
- Click on “Create new mission”.
- Within the “Create New Challenge” window, choose “ASP.NET Core Net API” from the checklist of templates displayed.
- Click on Subsequent.
- Within the “Arrange your new mission” window, specify the identify and placement of the brand new mission.
- Optionally, examine the “Place resolution and mission in the identical listing” checkbox, relying in your preferences.
- Click on Subsequent.
- Within the “Further Info” window proven under, uncheck the checkbox that claims “Use controllers…” as we shall be utilizing minimal APIs on this instance. Depart “Authentication Kind” as “None” (default).
- Be certain the “Allow Docker”, “Configure for HTTPS”, and “Allow Open API Help” checkboxes usually are not checked, as we can’t be utilizing any of these options right here.
- Click on Create.
We’ll use this ASP.NET Core 6 Net API mission to create minimal API endpoints and implement API versioning within the sections under.
Set up the API Versioning NuGet Packages
ASP.NET Core 6 minimal APIs assist versioning with any of those three packages:
- Asp.Versioning.Http – Used to assist versioning in minimal APIs.
- Asp.Versioning.Mvc – Used to assist model management in MVC Core functions.
- Asp.Versioning.Mvc.ApiExplorer – Used to offer assist within the API Explorer Extensions for versioning the ASP.NET Core API.
When you’ve efficiently created an ASP.NET Core 6 Net Utility mission in Visible Studio 2022, the subsequent factor it’s good to do is add the required NuGet packages to your mission. To do that, choose the mission within the Answer Explorer window, right-click and choose “Handle NuGet Packages…”. Within the NuGet Package deal Supervisor window, discover the next package deal and set up it.
Asp.Versioning.Http
Alternatively, you’ll be able to set up the package deal by the NuGet package deal supervisor console as proven under.
PM> Set up-Package deal Asp.Versioning.Http
Add API Versioning Help to Providers Assortment in ASP.NET Core 6
So as to add API versioning assist within the providers assortment for the minimal API, you will need to write the next code within the Program.cs file:
builder.Providers.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 the shopper will use to cross the API model when calling 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 you could mix HeaderApiVersionReader and QueryStringApiVersionReader to permit shoppers to specify model data in a number of methods when calling 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 configured model 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 set of variations 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 give attention to 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 yet one more instance of a minimal API").WithApiVersionSet(versionSet).WithApiVersionSet(versionSet)
.IsApiVersionNeutral();
Discover how we’ve got related the set of variations that we created within the earlier part. The MapToApiVersion technique maps a selected endpoint to a selected model. The IsApiVersionNeutral technique marks an endpoint as impartial for API versioning.
Right here is the complete 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.Providers.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();
Run the minimal API endpoints
Now press the F5 key within the Visible Studio 2022 IDE to run the app. The next screenshot (Determine 1) exhibits the error you may encounter for those who name the /getmessage endpoint with out specifying the API model key within the request header.
Determine 1. The /getmessage endpoint fails as a result of an API model is just not specified.
The next screenshot (Determine 2) exhibits what the output will appear to be while you specify the API model key within the request header and name the /getmessage endpoint once more.
Determine 2. The /getmessage endpoint succeeds as a result of the API model secret is specified within the request header.
As a result of the /gettext endpoint is marked API model impartial, you don’t want to specify an API model when calling this endpoint. If you run the /gettext endpoint, the response will appear to be the one proven within the screenshot (Determine 3) under.
Determine 3. The /gettext technique is executed with out having to specify an API model within the request header.
This being a minimal implementation, we’ve not used an information context, database, repository class, and even any mannequin class. We have now merely outlined two easy API endpoints for instance how we will create minimal API versioning in ASP.NET Core 6.
Copyright © 2022 IDG Communications, Inc.
I hope the article virtually Easy methods to model minimal APIs in ASP.NET Core 6 provides sharpness to you and is helpful for add-on to your information
How to version minimal APIs in ASP.NET Core 6