EndpointRegistration 1.0.3
dotnet add package EndpointRegistration --version 1.0.3
NuGet\Install-Package EndpointRegistration -Version 1.0.3
<PackageReference Include="EndpointRegistration" Version="1.0.3" />
paket add EndpointRegistration --version 1.0.3
#r "nuget: EndpointRegistration, 1.0.3"
// Install EndpointRegistration as a Cake Addin #addin nuget:?package=EndpointRegistration&version=1.0.3 // Install EndpointRegistration as a Cake Tool #tool nuget:?package=EndpointRegistration&version=1.0.3
Minimal API Endpoint AutoRegistration
via Source code generators to speedup development process. Without an expensive Reflection scan at the application startup.
Usage
- Add this nuget package
- Rebuild solution to start the code generator.
- Enable auto registration inside Program.cs
var registrations = app.UseEndpointAutoRegister();
This extension method returns an Registrations
object that contains properties with all endpoints registrations as RouteHandlerBuilder
to enable another configuration (see below).
registrations.ProductDetailEndpoint.Produces(200)
Endpoints
Create a class that implements following rules.
You can skip an endpoint by using IgnoreEndpoint
attribute.
Interface Handler-based endpoint
Implement one of exposed interfaces
IApiEndpoint
defaults to HTTP GETIApiGetEndpoint
IApiPutEndpoint
IApiPostEndpoint
IApiDeleteEndpoint
All interfaces define a Handler
method that handles HTTP request.
Additionally a class can implement IRoutePattern
or IApiRouteEndpoint
.
Both add a Pattern
property used for a route template.
Conventional Name Handler-based endpoints
The source code generator looks for a classes named with a suffix
Endpoint
defaults to HTTP GETGetEndpoint
PostEndpoint
PutEndpoint
DeleteEndpoint
The class has to declare Handler
method.
The class could be even static with static method.
Register-based endpoint
It's the most powerful options how to declare an api endpoint. It could be either conventional or interface based:
- Implements
IAutoRegisterApiEndpoint
- Name ends with one of these suffixes
AutoRegisterApiEndpoint
,ArApiEndpoint
The class has to declare Register
method. It could be static as well.
Endpoint structure
An endpoint is a class declaring:
- Handler-based endpoints
Handler
method (required) - handling HTTP requestsPattern
property (optional) - a route template declarationConfigure
method (optional) - an additional configuration of createdRouteHandlerBuilder
- Register-based endpoints
RouteHandlerBuilder Register(IEndpointRouteBuilder app)
method (required) - should contain a complete endpoint declaration (Program.cs)
Conventions
Endpoint name
A classname without endpoint suffix.
DetailGetEndpoint ⇒ Detail
Applies to all endpoint types.
Route pattern
Ordered resolution:
Pattern
property if not nullRouteAttribute
applied on theHandler
method- Endpoint name-based convetional
- Endpoint name (without the suffix) splited into words from the PascalCase
- Appended
Handler
's arguments with appliedFromRouteAttribute
in same order - If the first argument of
Handler
method is of primitive type (or string) then is used it as default
Applies to the Handler-based endpoints.
Additional route configuration
If you want to configure a created RouteHandlerBuilder
then declare Configure
method.
You can use it to define an Action name or a 'Produces' info.
void Configure(RouteHandlerBuilder eb) ⇒ eb.WithName("Action1")
Applies to the Handler-based endpoints.
Examples
Endpoint
Static class conventional name (Handler-base)
public static class DetailGetEndpoint
{
public static void Configure(RouteHandlerBuilder eb) => eb.WithName("ProductDetail");
public static string Pattern => "/Detail/{id}";
public static string Handler(int id)
{
return $"Should return product with id: {id}";
}
}
Interface (Handler-based)
public class ListApiEndpoint : IApiEndpoint
{
public void Configure(RouteHandlerBuilder eb)
{
eb
.Produces(200)
.Produces(404);
}
public string Pattern => "/list/index";
public Delegate Handler { get; } = (HttpContext c) => { return $"Hello from {nameof(ListApiEndpoint)}: {c.Request.Path}"; };
}
Registration based
public static class DetailArApiEndpoint
{
public static RouteHandlerBuilder Register(IEndpointRouteBuilder app)
{
return app
.MapGet("/Detail/{id}", () => $"Should return product with id: {id}")
.Produces(200);
}
}
Route patterns conventions
Endpoint name + FromRouteAttribute
public class DetailGetEndpoint
{
public Delegate Handler => (int id, object o, MyClass c, [FromRoute] int r, [FromRoute(Name = "param-s")] int s) => $"id: {id}";
}
creates app.MapGet("/detail1/{r}/{param-s}", <handler>)
Endpoint name (multiple word) + first primitive argument
public class ProductsDetailGetEndpoint
{
public Delegate Handler => (int id) => $"id: {id}";
}
creates app.MapGet("/products/info/{id}", <handler>)
Supported declarations
Handler method
public Delegate Handler => (int id) => $"id: {id}";
public string Handler(int id) => $"id: {id}";
public static string Handler(int id) { return $"id: {id}"; }
Route Pattern property
public string Pattern => "/detail/{id}";
public string Pattern { get; } = "/detail/{id}";
public string Pattern { get { return "/detail/{id}"; } }
public string Pattern { get => "/detail/{id}"; }
public static string Pattern => "/Detail/{id}";
same for static
Configure method
public void Configure(RouteHandlerBuilder eb){ eb.Produces(200); }
public string Configure(RouteHandlerBuilder eb) => eb.Produces(200);";
public static string Configure => (RouteHandlerBuilder eb) => eb.Produces(200);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
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 |
---|---|---|
1.0.3 | 443 | 5/2/2022 |
Fixed usage of fullname of IgnoreEndpoint Attribute; Code analysis asseblies - <PrivateAssets>all</PrivateAssets>