Source code generator for an endpoint auto registration.
$ dotnet add package EndpointRegistrationvia Source code generators to speedup development process. Without an expensive Reflection scan at the application startup.
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)
Create a class that implements following rules.
You can skip an endpoint by using IgnoreEndpoint attribute.
Implement one of exposed interfaces
IApiEndpoint defaults to HTTP GETIApiGetEndpointIApiPutEndpointIApiPostEndpointIApiDeleteEndpointAll 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.
The source code generator looks for a classes named with a suffix
Endpoint defaults to HTTP GETGetEndpointPostEndpointPutEndpointDeleteEndpointThe class has to declare Handler method.
The class could be even static with static method.
It's the most powerful options how to declare an api endpoint. It could be either conventional or interface based:
IAutoRegisterApiEndpointAutoRegisterApiEndpoint, ArApiEndpointThe class has to declare Register method. It could be static as well.
An endpoint is a class declaring:
Handler method (required) - handling HTTP requestsPattern property (optional) - a route template declarationConfigure method (optional) - an additional configuration of created RouteHandlerBuilderRouteHandlerBuilder Register(IEndpointRouteBuilder app) method (required) - should contain a complete endpoint declaration (Program.cs)A classname without endpoint suffix.
DetailGetEndpoint => Detail
Applies to all endpoint types.
Ordered resolution:
Pattern property if not nullRouteAttribute applied on the Handler methodHandler's arguments with applied FromRouteAttribute in same orderHandler method is of primitive type (or string) then is used it as defaultApplies to the Handler-based endpoints.
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.
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}";
}
}
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}"; };
}
public static class DetailArApiEndpoint
{
public static RouteHandlerBuilder Register(IEndpointRouteBuilder app)
{
return app
.MapGet("/Detail/{id}", () => $"Should return product with id: {id}")
.Produces(200);
}
}
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>)
public class ProductsDetailGetEndpoint
{
public Delegate Handler => (int id) => $"id: {id}";
}
creates app.MapGet("/products/info/{id}", <handler>)
public Delegate Handler => (int id) => $"id: {id}";public string Handler(int id) => $"id: {id}";public static string Handler(int id) { return $"id: {id}"; }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 staticpublic void Configure(RouteHandlerBuilder eb){ eb.Produces(200); }public string Configure(RouteHandlerBuilder eb) => eb.Produces(200);";public static string Configure => (RouteHandlerBuilder eb) => eb.Produces(200);