.NET7 Preview4之MapGroup
这篇是“闻(看)香(码)识(学)女(技)人(术)”。
这也是一个有意思的功能,路由分组,啥也不说了,看代码看结果:
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.OpenApi;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/test", () =>
{
})
.WithTags("TestApi");
app.MapPost("/test", () =>
{
})
.WithTags("TestApi");
app.MapGroup("/data").MapDataApi();//.RequireAuthorization();
app.Run();
/// <summary>
/// 分组扩展类
/// </summary>
public static class DataApi
{
public static GroupRouteBuilder MapDataApi(this GroupRouteBuilder group)
{
group.MapGet("/", Query);
group.MapDelete("/{id}", Remove);
group.MapPost("/", Add);
group.MapPut("/", Modify).WithOpenApi();
return group;
}
/// <summary>
/// 查询数据
/// </summary>
/// <returns></returns>
public static async Task<JsonHttpResult<List<Data>>> Query()
{
await Task.Delay(TimeSpan.FromMicroseconds(1));
return TypedResults.Json<List<Data>>(new List<Data> {
new Data { ID = 1, Name = "test01" },
new Data { ID = 2, Name = "test02" },
});
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static async Task<Ok> Remove(int id)
{
await Task.Delay(1);
return TypedResults.Ok();
}
/// <summary>
/// 添加
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static async Task<Created<string>> Add(Data data)
{
await Task.Delay(1);
return TypedResults.Created<string>("/", "ok");
}
/// <summary>
/// 修改
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static async Task<Ok> Modify(Data data)
{
await Task.Delay(1);
return TypedResults.Ok();
}
}
public class Data
{
public int ID { get; set; }
public string? Name { get; set; }
}
看图:
想要更快更方便的了解相关知识,可以关注微信公众号
****欢迎关注我的asp.net core系统课程****
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524