当前位置 : 主页 > 网络编程 > net编程 >

.NET7 Preview4之MapGroup

来源:互联网 收集:自由互联 发布时间:2023-09-03
这篇是“闻(看)香(码)识(学)女(技)人(术)”。 这也是一个有意思的功能,路由分组,啥也不说了,看代码看结果: using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.OpenApi; var build

  这篇是“闻(看)香(码)识(学)女(技)人(术)”。

  这也是一个有意思的功能,路由分组,啥也不说了,看代码看结果:

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; }
}

看图:

.NET7 Preview4之MapGroup_MapGroup

  想要更快更方便的了解相关知识,可以关注微信公众号

.NET7 Preview4之MapGroup_MapGroup_02

【本文转自:国外高防服务器 http://www.558idc.com/usa.html转载请说明出处】
上一篇:.NET7 Preview4:重构的返回值TypedResults
下一篇:没有了
网友评论