aspnet-minimal-api

v2026.09.24

ASP.NET Core Minimal API endpoints, route groups, filters, and typed results. Covers modern .NET 8+ endpoint patterns. USE WHEN: user mentions "Minimal API", "MapGet", "MapPost", "route groups", "endpoint filters", ".NET minimal", "lambda endpoints" DO NOT USE FOR: Controller-based APIs (use `aspnet-core`), Express.js (use `express`), FastAPI (use `fastapi`)

GitHub
安装命令
npx skhub add claude-dev-suite/aspnet-minimal-api
Markdown
SKILL.md

ASP.NET Minimal API - Quick Reference

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: aspnet-core, topic: minimal-api for comprehensive documentation.

Basic Endpoints

var app = WebApplication.Create(args);

app.MapGet("/api/users", async (IUserService service) =>
    Results.Ok(await service.GetAllAsync()));

app.MapGet("/api/users/{id:int}", async (int id, IUserService service) =>
    await service.GetByIdAsync(id) is { } user
        ? Results.Ok(user)
        : Results.NotFound());

app.MapPost("/api/users", async (CreateUserRequest request, IUserService service) =>
{
    var user = await service.CreateAsync(request);
    return Results.Created($"/api/users/{user.Id}", user);
});

app.MapPut("/api/users/{id:int}", async (int id, UpdateUserRequest request, IUserService service) =>
    await service.UpdateAsync(id, request) ? Results.NoContent() : Results.NotFound());

app.MapDelete("/api/users/{id:int}", async (int id, IUserService service) =>
{
    await service.DeleteAsync(id);
    return Results.NoContent();
});

app.Run();

Route Groups

var users = app.MapGroup("/api/users")
    .WithTags("Users")
    .RequireAuthorization();

users.MapGet("/", GetAll);
users.MapGet("/{id:int}", GetById);
users.MapPost("/", Create);

// Nested groups
var admin = app.MapGroup("/api/admin")
    .RequireAuthorization("AdminOnly");

admin.MapGroup("/users").MapGet("/", GetAllUsers);

Endpoint Filters

// Validation filter
public class ValidationFilter<T> : IEndpointFilter where T : class
{
    public async ValueTask<object?> InvokeAsync(
        EndpointFilterInvocationContext context,
        EndpointFilterDelegate next)
    {
        var argument = context.Arguments.OfType<T>().FirstOrDefault();
        if (argument is null)
            return Results.BadRequest("Invalid request body");

        var validator = context.HttpContext.RequestServices.GetService<IValidator<T>>();
        if (validator is not null)
        {
            var result = await validator.ValidateAsync(argument);
            if (!result.IsValid)
                return Results.ValidationProblem(result.ToDictionary());
        }

        return await next(context);
    }
}

// Apply filter
users.MapPost("/", Create).AddEndpointFilter<ValidationFilter<CreateUserRequest>>();

TypedResults (.NET 7+)

app.MapGet("/api/users/{id:int}", async Task<Results<Ok<UserResponse>, NotFound>> (int id, IUserService service) =>
    await service.GetByIdAsync(id) is { } user
        ? TypedResults.Ok(user)
        : TypedResults.NotFound());

Anti-Patterns

Anti-PatternWhy It's BadCorrect Approach
All endpoints in Program.csHard to maintainUse route groups and extension methods
No validationAccepts bad inputUse endpoint filters
Inline business logicNot testableInject services
Not using TypedResultsNo OpenAPI metadataUse TypedResults for typed responses

Quick Troubleshooting

IssueLikely CauseSolution
Parameter not boundWrong type or nameUse explicit [FromRoute] / [FromQuery]
Filter not executingNot registeredAdd with AddEndpointFilter<T>()
OpenAPI missing typesUsing Results.Ok()Use TypedResults.Ok()
Route conflictsAmbiguous routesAdd route constraints like {id:int}
发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

2026年9月24日

分类

未分类

许可证

MIT

源路径

skills/backend-frameworks/aspnet-minimal-api

默认分支

main

最新提交

9496306

Tree SHA

fe4e2f1