using Microsoft.EntityFrameworkCore; public static class ProductsModule { public static void RegisterProductsEndpoints(this IEndpointRouteBuilder endpoints) { endpoints.MapGet("/products", async (MemberDbContext dbContext) => { return Results.Ok(await dbContext.members.ToListAsync()); }); endpoints.MapPost("/products", async (Member member, MemberDbContext dbContext) => { dbContext.members.Add(member); await dbContext.SaveChangesAsync(); return Results.Ok(member); }); } }
在program中使用
#region MiniApi app.RegisterProductsEndpoints(); #endregion
使用Carter,nuget搜索。把所有的MiniAPI都Map进去
public class ProductsModule : ICarterModule { public void AddRoutes(IEndpointRouteBuilder app) { app.MapGet("/products", async (MemberDbContext dbContext) => { return Results.Ok(await dbContext.members.ToListAsync()); }); app.MapPost("/products", async (Member member, MemberDbContext dbContext) => { dbContext.members.Add(member); await dbContext.SaveChangesAsync(); return Results.Ok(member); }); } }
Program中使用
builder.Services.AddCarter(); // Carter用来注入MiniApi app.MapCarter();
标签:MiniAPI,Ok,netcore,app,dbContext,member,使用,return,public From: https://www.cnblogs.com/Insist-Y/p/17437882.html