Minimal API - Endpoints (终结点)

51 阅读1分钟

常用 Http 请求方法

app.MapGet("/todos", () => []);

app.MapPost("/todos", (Todo todo) => {});

app.MapPut("/todos/{id}", (int id, Todo todo) => {});

app.MapDelete("/todos/{id}", (int id) => {});

绑定多个 Http 请求方法

app.MapMethods("/options-or-head", new[] { "OPTIONS", "HEAD" }, () => "This is an options or head request ");

MapGroup API

MapGroup支持为多个Endpoint提供一致处理,如上文中的路由,统一鉴权、中间件、filter等等。这也给开发者组织代码提供了便捷。

上文代码修改

var group = app.MapGroup("/todos");

group.MapGet("", () => []);

group.MapPost("", (Todo todo) => {});

group.MapPut("{id}", (int id, Todo todo) => {});

group.MapDelete("{id}", (int id) => {});

统一鉴权

var group = app.MapGroup("/todos").RequireAuthorization();