HamedStack.AspNetCore.Endpoint
1.0.1
See the version list below for details.
dotnet add package HamedStack.AspNetCore.Endpoint --version 1.0.1
NuGet\Install-Package HamedStack.AspNetCore.Endpoint -Version 1.0.1
<PackageReference Include="HamedStack.AspNetCore.Endpoint" Version="1.0.1" />
paket add HamedStack.AspNetCore.Endpoint --version 1.0.1
#r "nuget: HamedStack.AspNetCore.Endpoint, 1.0.1"
// Install HamedStack.AspNetCore.Endpoint as a Cake Addin #addin nuget:?package=HamedStack.AspNetCore.Endpoint&version=1.0.1 // Install HamedStack.AspNetCore.Endpoint as a Cake Tool #tool nuget:?package=HamedStack.AspNetCore.Endpoint&version=1.0.1
HamedStack.AspNetCore.Endpoint
This documentation provides detailed instructions for handling minimal API endpoints in an ASP.NET Core application using the latest version of ASP.NET Core. It introduces the IMinimalApiEndpoint
interface for defining endpoint handlers and the MinimalApiEndpointsExtensions
class for registering and mapping these handlers.
IMinimalApiEndpoint Interface
Summary
Defines a contract for minimal API endpoint handlers. Implementations of this interface should define the logic for handling an API endpoint.
Members
HandleEndpoint
void HandleEndpoint(WebApplication app);
Parameters
app
: TheWebApplication
instance used to configure the endpoint.
Description
Handles the registration of the endpoint with the specified endpoint route builder. Implementing classes should provide the specific logic for configuring the endpoint.
MinimalApiEndpointsExtensions Class
Summary
Provides extension methods to register and map minimal API endpoints within an ASP.NET Core application.
Methods
AddMinimalApiEndpoints
public static IServiceCollection AddMinimalApiEndpoints(this IServiceCollection services)
Parameters
services
: TheIServiceCollection
to add the services to.
Returns
IServiceCollection
: The service collection for chaining.
Description
Registers all implementations of IMinimalApiEndpoint
found in the application into the service collection. This method scans the application for types that implement the IMinimalApiEndpoint
interface and registers them as transient services.
Example Usage
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMinimalApiEndpoints();
MapMinimalApiEndpoints
public static WebApplication MapMinimalApiEndpoints(this WebApplication app)
Parameters
app
: TheWebApplication
to map the endpoints to.
Returns
WebApplication
: The web application for chaining.
Description
Maps all registered minimal API endpoints into the WebApplication
, enabling their routes. This method retrieves all services that implement the IMinimalApiEndpoint
interface and invokes their HandleEndpoint
method to configure the endpoints.
Example Usage
var app = builder.Build();
app.MapMinimalApiEndpoints();
app.Run();
Implementation Example
Below is a complete example demonstrating how to implement and use the minimal API endpoint handler system in an ASP.NET Core application.
Step 1: Define an Endpoint Handler
Create a class that implements the IMinimalApiEndpoint
interface.
public class HelloWorldEndpoint : IMinimalApiEndpoint
{
public void HandleEndpoint(WebApplication app)
{
app.MapGet("/hello", () => "Hello, World!");
}
}
Step 2: Register the Endpoint Handlers
In your program setup, register the endpoint handlers using the provided extension method.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMinimalApiEndpoints();
Step 3: Configure the Application
Ensure your application maps the registered endpoints and runs correctly.
var app = builder.Build();
app.MapMinimalApiEndpoints();
app.Run();
Full Example
Combining all steps into a complete example:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public interface IMinimalApiEndpoint
{
void HandleEndpoint(WebApplication app);
}
public class HelloWorldEndpoint : IMinimalApiEndpoint
{
public void HandleEndpoint(WebApplication app)
{
endpoints.MapGet("/hello", () => "Hello, World!");
}
}
public static class MinimalApiEndpointsExtensions
{
public static IServiceCollection AddMinimalApiEndpoints(this IServiceCollection services)
{
var endpointTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(IMinimalApiEndpoint).IsAssignableFrom(type) && type is { IsInterface: false, IsAbstract: false });
foreach (var type in endpointTypes)
{
services.AddTransient(typeof(IMinimalApiEndpoint), type);
}
return services;
}
public static WebApplication MapMinimalApiEndpoints(this WebApplication app)
{
var endpoints = app.Services.GetServices<IMinimalApiEndpoint>();
foreach (var endpoint in endpoints)
{
endpoint.HandleEndpoint(app);
}
return app;
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMinimalApiEndpoints();
var app = builder.Build();
app.MapMinimalApiEndpoints();
app.Run();
Conclusion
By following the outlined steps and utilizing the provided interface and extension methods, you can create a scalable and maintainable system for managing minimal API endpoints in your ASP.NET Core application. This approach allows for a clean separation of endpoint configuration logic and centralizes the registration and mapping of endpoints.
Product | Versions 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 is compatible. 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 is compatible. 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. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.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.