ASP.NET Core 6 presents a streamlined hosting design that enables us to develop lightweight APIs with very little dependencies. We have actually talked about getting started with minimal APIs, using logging and reliance injection with minimal APIs, and testing very little APIs in previous article here. In this article, we’ll examine how we can carry out versioning for our minimal API endpoints.We’ll highlight how we can variation minimal APIs in ASP.NET 6 Core by following this series of 6 actions:
- Produce a ASP.NET 6 Core Very little API task in Visual Studio 2022
- Set up the essential NuGet package(s)
- Add API versioning assistance to our project
- Develop a variation set
- Produce API endpoints and associate the version set
- Carry out the API endpoints in Postman
To deal with the code examples provided in this article, you must have Visual Studio 2022 installed in your system. If you don’t currently have a copy, you can download Visual Studio 2022 here.
Develop an ASP.NET Core 6 very little Web API job in Visual Studio 2022
First of all, let’s develop an ASP.NET Core 6 project in Visual Studio. Following these actions will create a new ASP.NET Core 6 Web API project in Visual Studio 2022:
- Introduce the Visual Studio 2022 IDE.
- Click on “Create brand-new project.”
- In the “Produce brand-new project” window, choose “ASP.NET Core Web API” from the list of templates displayed.
- Click Next.
- In the “Configure your brand-new task” window, define the name and place for the new project.
- Additionally check the “Place solution and job in the exact same directory site” check box, depending on your preferences.
- Click Next.
- In the “Additional Information” window revealed next, uncheck the check box that says “Usage controllers …” because we’ll be utilizing very little APIs in this example. Leave the “Authentication Type” as “None” (default).
- Guarantee that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are untreated as we won’t be using any of those functions here.
- Click Produce.
We’ll use this ASP.NET Core 6 Web API project to develop very little API endpoints and implement API versioning in the sections below.Install the API
versioning NuGet packages ASP.NET Core 6 very little APIs support versioning using any of these 3 packages: Asp.Versioning.Http– used to supply assistance for versioning in very little APIs. Asp.Versioning.Mvc– used to provide assistance for versioning in
Add API versioning assistance to the services collection in ASP.NET Core 6 To add API versioning assistance in the services collection for the very little API, you should write the following code in the Program.cs file: builder.Services.AddApiVersioning (options => ); Keep in mind how the default API variation has been defined. The ApiVersionReader home is utilized to specify the key that will be utilized by the client to pass the API version when calling the API endpoints. When the worth of the AssumeDefaultVersionWhenUnspecified residential or commercial property is true, the default API variation will be used if the customer has not defined an API version.Note that you can combine HeaderApiVersionReader and QueryStringApiVersionReader to make it possible for clients to define variation information in several methods when calling the API endpoints.services.AddApiVersioning(choices=>
); Add API versions utilizing a version
embeded in ASP.NET Core 6 Now define a new version set for your API using the following code. var versionSet=app.NewApiVersionSet(). HasApiVersion ( 1.0). HasApiVersion(2.0). ReportApiVersions (). Build(); We’ll use this variation set in the next section to create our API endpoints.Create minimal API endpoints in ASP.NET Core 6 We’ll develop two endpoints here to keep it simple and focus on versioning our very little APIs. Compose the following code in
the Program.cs file to produce two endpoints.app.MapGet(“/ GetMessage”,() =>
“This is an example of a very little API “). WithApiVersionSet (versionSet). MapToApiVersion(1.0); app.MapGet ( “/ GetText “,( )
= >”This is yet another example of a very little API”). WithApiVersionSet(versionSet ). WithApiVersionSet (versionSet ).
IsApiVersionNeutral (); Note how we have associated
the version set that we produced in the previous section. The MapToApiVersion method maps a specific endpoint to a particular version. The IsApiVersionNeutral approach marks an endpoint as neutral to API versioning.Here is the complete source code of the Program.cs file for your recommendation: using Asp.Versioning;
using Asp.Versioning.Conventions; var contractor =WebApplication.CreateBuilder (args);// Include services to the container.builder.Services.AddApiVersioning(options=> options.DefaultApiVersion=new ApiVersion(1,
0); options.ReportApiVersions =true; options.AssumeDefaultVersionWhenUnspecified=real; options.ApiVersionReader=brand-new HeaderApiVersionReader(“api-version”); ); var app=builder.Build( ); var versionSet =app.NewApiVersionSet( ).
HasApiVersion(1.0). HasApiVersion (2.0). ReportApiVersions (). Build()
;// Set up the HTTP demand pipeline.app.MapGet(“/ GetMessage”,()=>”This is an example of a very little API” ). WithApiVersionSet(versionSet ). HasApiVersion (new ApiVersion
(2, 0)); app.MapGet(“/ GetText”,()= >
“This is another example of a minimal API”). WithApiVersionSet(versionSet). IsApiVersionNeutral ();
app.Run (); Carry out the minimal API endpoints Now press the F5 type in
Visual Studio 2022 IDE to run the application. The following screenshot(Figure 1)reveals the error you will encounter if you call the/ getmessage endpoint without specifying the API version key in the request header.< img alt="aspnet core api versioning 01"width="1200"height=" 618 "src="https://images.idgesg.net/images/article/2022/08/aspnet-core-api-versioning-01-100931780-large.jpg?auto=webp&quality=85,70"/ > IDG Figure 1. The/ getmessage endpoint throws a mistake due to the fact that no API variation is defined. The next screenshot (Figure 2)shows how the output will look when you specify the API variation key in the demand header and call the/ getmessage endpoint once again. IDG Figure 2. The/ getmessage endpoint runs successfully because the API version key is specified in the demand header. Because the/ gettext endpoint is marked as API variation neutral, you do
n’t require to define an API version when calling this endpoint. When you execute the/ gettext endpoint, the response will appear like that shown in the screenshot(Figure 3)listed below. IDG Figure 3. The/ gettext technique performs without needing to specify an API variation in the request header. This being a minimal execution, we have not utilized a data context, a database, a repository class, or perhaps any design classes. We have actually simply defined two basic API endpoints to show how we can version very little APIs in ASP.NET Core 6. Copyright © 2022 IDG Communications, Inc. Source