ASP.NET Core7: Use endpoint groups to manage minimal APIs versioning
source link: https://anthonygiretti.com/2023/03/16/asp-net-core7-use-endpoint-groups-to-manage-minimal-apis-versioning/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ASP.NET Core7: Use endpoint groups to manage minimal APIs versioning
Introduction
This year .NET 7 ships with ASP.NET Core 7 and the latter brings big improvements on Minimal APIs. Let me introduce you the endpoint groups feature, which applies on Minimal APIs only. Endpoint groups are a way to declare some common routing information across several minimal endpoints, but more also, like constraints such as Authorization, CORS rules etc etc…. We will see in this post what you can do with endpoint groups and how useful it could be.
What are endpoint groups
Endpoint groups allow you to apply constraints and rules on a bunch of minimal endpoints. ASP.NET Core 7 provides a RouteGroupBuilder class to build your rules and group your endpoints. Let’s create two groups of endpoint, each one contains two GET endpoints, materialized with the MapGet method:
I strongly suggest you, for readability purpose to group only endpoints in separate extension methods, like above, and not to apply any constraint there. If you wanna add constraints/rules and base route you can do it directly in the Program.cs file, so, by reading your endpoint groups, you’ll see fastly what group has what constraint/rule or base route. The following code shows how to add constraints, rules and base route:
As you can see, group one has a CORS policy applied on it while group two has another CORS policy, Authorization and RateLimiting rules.
Use endpoint groups to manage minimal API versioning
Endpoint groups allow you to manage API versioning. Simply add a base route on the MapGroup method, example: MapGroup(“/v1”) or MapGroup(“/v2”). To make it work with Swagger add the following Nuget package:
NuGet\Install-Package Microsoft.AspNetCore.OpenApi -Version 7.0.4
Then add the following extensions on the IServiceCollection builder:
- AddEndpointsApiExplorer();
- AddSwaggerGen();
Note that the AddEndpointsApiExplorer() extension method is made to make Minimal APIs visible to Swagger.
Once done add the following Swagger middlewares on the ASP.NET Core pipeline:
- UseSwagger();
- UseSwaggerUI();
To make your Swagger documentation clearer (clear distinction between endpoint groups in SwaggerUI) you can use the WithTags extensions methods and name them as follow:
If you lunch the following URL (which is defined like this by default) : /swagger/index.html you should see something like this:
Like this:
Recommend
-
91
-
5
Versioning REST APIsAbout Me Hi, my name is Irina Scurtu. I am a Microsoft MVP for developer technologies, Software Architect, passionate about .NET and the world around it....
-
4
#Microsoft #Azure #CostManagementHow to...
-
10
#Microsoft #Surface #ManagementManage All...
-
7
November 14, 2022Minimal API validation with ASP.NET 7.0 Endpoint FiltersIf you are still getting to grips with Minimal APIs I did a how-to on moving from MVC to Minimal APIs
-
7
ASP.NET Core 7: Introducing endpoint filters, actions filters for minimal APIs 2023-03-19 by anthonygiretti Introduction...
-
3
Tim Deschryver BLOG RSS ...
-
2
Semantic Versioning for Express APIsWhat is Semantic Versioning?
-
7
October 24, 2023 Global Endpoint Filters with ASP.NET Core Minimal APIs
-
3
Dhara Thakkar on X: "Our paper titled "The Minimal Faithful Permutation Degree of Groups without Abelian Normal Subgroups" has been accepted in #STOC2024🥳 Thanks to Prof Bireswar Das. I also thank to Prof. Derek Holt and Prof. @joshuagrochow f...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK