NetCoreApiExtensions.WebApi 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package NetCoreApiExtensions.WebApi --version 2.0.2
NuGet\Install-Package NetCoreApiExtensions.WebApi -Version 2.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetCoreApiExtensions.WebApi" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetCoreApiExtensions.WebApi --version 2.0.2
#r "nuget: NetCoreApiExtensions.WebApi, 2.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetCoreApiExtensions.WebApi as a Cake Addin
#addin nuget:?package=NetCoreApiExtensions.WebApi&version=2.0.2

// Install NetCoreApiExtensions.WebApi as a Cake Tool
#tool nuget:?package=NetCoreApiExtensions.WebApi&version=2.0.2

NetCoreApiExtensions.WebApi


This package provides an opinionated way to create versioned APIs by convention. There are 2 main parts for implementing this pattern. First, making the application look for the convetion and second, connecting controllers to the convention.

There are other features and usages that are not fully documented here, but this is a fast-track primer to setting things up.

BuildNetCoreApi (Extension)

Parameter Type Default Usage
apiTitle string Tells SwaggerUI what title to display for your API
groupNameFormat string "'v'VVV" Tells the application how to format your API versions. Supports ApiVersionFormatProvider formats.

VersionedApiControllerAttribute

Provides the basic versioning for an API controller. Inherits ApiControllerAttribute and supplies a versioned route template.

Parameter Type Default Usage
rootResourceSegments params string[] If supplied, parameters are split and added to the end of the template, joined by /

Route Template without rootResourceSegments:

$"api/v{{version:apiVersion}}/[Controller]"

Route Template with rootResourceSegments:

$"api/v{{version:apiVersion}}/[Controller]/{{string.Join('/', pathSegments)}}

Usage

There are many extensions methods provided by this package that can be used to tailor the experience, but the fastest (and most consistent) way to get started is by using the top-level extension that acts as a WebApplication factory and replaces the standard Build() call.

The API project stubs this:

var app = builder.Build();

you should replace that line with:

var app = builder.BuildNetCoreApi("<your API Title>");

Substitute in the name that you want displayed in SwaggerUI as the parameter to the method. Since this replaces the builder.Build() call and does that internally, this should be the LAST thing you do before you continuing to configure the app.

What's in it for you?

Less Configuration

Using the Asp.Net Core Web Api for .Net 6 creates a program.cs that looks like this:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

With this package, that can be reduced to:

using NetCoreApiExtensions.WebApi.Extensions;

var builder = WebApplication.CreateBuilder(args);

var app = builder.BuildNetCoreApi("Demo API");

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Convention-based API Versioning

This package brings in some MVC dependencies that allow your endpoints to be versioned by convention. This does require that your API be organized in a specific way.

Folder Struture
Api.Project
    - v1_0
        - Controllers
            - WeatherForcastController.cs
    - v2_2
        - Controllers
            - WeatherForcastController.cs
    - v3_0-beta
        - Controllers
            - WeatherForcastController.cs

This folder structure will create 3 versions of the WeatherForcastController along with 3 distinct versions (selectable from the dropdown) in SwaggerUI:

v1, v2.2, and v3-beta

Change Log


2.0.2

Added

  • Extensions to configure Web APIs
  • Documentation on usage
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 378 11/14/2022
2.1.2 311 11/7/2022
2.1.1 338 11/4/2022
2.1.0 342 10/30/2022
2.0.2 361 10/30/2022
2.0.1 365 10/29/2022
2.0.0 352 10/28/2022
1.1.0 390 8/8/2022
1.0.1 304 5/11/2021
1.0.0 286 5/11/2021

Added opinionated convention-based API Wire-up extensions.