How to variation very little APIs in ASP.NET Core 6

Uncategorized

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:

  1. Produce a ASP.NET 6 Core Very little API task in Visual Studio 2022
  2. Set up the essential NuGet package(s)
  3. Add API versioning assistance to our project
  4. Develop a variation set
  5. Produce API endpoints and associate the version set
  6. 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:

  1. Introduce the Visual Studio 2022 IDE.
  2. Click on “Create brand-new project.”
  3. In the “Produce brand-new project” window, choose “ASP.NET Core Web API” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your brand-new task” window, define the name and place for the new project.
  6. Additionally check the “Place solution and job in the exact same directory site” check box, depending on your preferences.
  7. Click Next.
  8. 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).
  9. 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.
  10. 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

  • MVC Core apps. Asp.Versioning.Mvc.ApiExplorer– utilized to offer support in the API explorer extensions for ASP.NET Core API versioning If you have actually effectively created an ASP.NET Core 6 web application job in Visual Studio 2022, the next thing you need to do is include the essential NuGet bundles to your job. To do this, select the job in the Service Explorer window, right-click, and choose” Handle NuGet Bundles …”In the NuGet Package Supervisor window, search for the following bundle and install it.Asp.Versioning.Http Additionally, you can install the plan by means of the NuGet Bundle Manager Console as revealed below. PM > Install-Package Asp.Versioning.Http

    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 isaspnet core api versioning 01 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.aspnet core api versioning 02 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 Coreaspnet core api versioning 03 6. Copyright © 2022 IDG Communications, Inc. Source

  • Leave a Reply

    Your email address will not be published. Required fields are marked *