diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index e58520638..5c555bd6e 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -147,8 +147,8 @@ builder.Services.AddMasaMinimalAPIs(options => options.PutPrefixes = new List { "Put", "Update" }; options.DeletePrefixes = new List { "Delete", "Remove" }; - options.RouteHandlerBuilder= t=> { - t.RequireAuthorization().AddEndpointFilter(); + options.RouteHandlerBuilder= t=> { + t.RequireAuthorization().AddEndpointFilter().WithGroupName("Institution"); }; options.DisableTrimMethodPrefix = true; //禁用去除方法前缀 options.DisableAutoMapRoute = false;//可通过配置true禁用全局自动路由映射或者删除此配置以启用全局自动路由映射 diff --git a/IRaCIS.Core.API/_ServiceExtensions/SwaggerSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/SwaggerSetup.cs index 4b0642670..d2f85d734 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/SwaggerSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/SwaggerSetup.cs @@ -43,70 +43,6 @@ public enum SwaggerVersion public static class SwaggerSetup { - public static void AddSwaggerSetupOld(this IServiceCollection services) - { - services.AddEndpointsApiExplorer(); - - services.AddSwaggerGen(options => - { - typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList() - .ForEach(field => - { - var description = field.GetCustomAttribute()?.Description ?? field.Name; - options.SwaggerDoc(field.Name, new Microsoft.OpenApi.Models.OpenApiInfo - { - Version = field.Name, - Description = $"{field.Name} API", - Title = description // 使用Description作为Title - }); - }); - - // 接口排序 - options.OrderActionsBy(o => o.GroupName); - - options.DocInclusionPredicate((docName, apiDes) => - { - if (!apiDes.TryGetMethodInfo(out MethodInfo methodInfo)) return false; - var versions = methodInfo.DeclaringType.GetCustomAttributes(true) - .OfType() - .Select(attr => attr.GroupName); - - return versions.Any(v => v.ToString() == docName); - }); - - - //添加注释 - var basePath = AppContext.BaseDirectory; - var xmlPath1 = Path.Combine(basePath, "IRaCIS.Core.Application.xml"); - var xmlPath2 = Path.Combine(basePath, "IRaCIS.Core.API.xml"); - options.IncludeXmlComments(xmlPath1, true); - options.IncludeXmlComments(xmlPath2, true); - - // 在header中添加token,传递到后台 - options.OperationFilter(); - - - // 添加登录按钮 - options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme() - { - Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", - Name = "Authorization", - - //In = "header", - //Type = "apiKey" - }); - - //// 添加登录按钮 - //options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme() - //{ - // Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", - // Name = "Authorization", - - // //In = "header", - // //Type = "apiKey" - //}); - }); - } public static void AddSwaggerSetup(this IServiceCollection services) { services.AddEndpointsApiExplorer(); diff --git a/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs b/IRaCIS.Core.Application/BusinessFilter/LegacyController/UnifiedApiResultFilter.cs similarity index 75% rename from IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs rename to IRaCIS.Core.Application/BusinessFilter/LegacyController/UnifiedApiResultFilter.cs index 320d80803..a1997eabc 100644 --- a/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/LegacyController/UnifiedApiResultFilter.cs @@ -126,56 +126,5 @@ public class UnifiedApiResultFilter : Attribute, IAsyncResultFilter #endregion -#region minimalapi 流程 -public class UnifiedApiResultEndpointFilter : IEndpointFilter -{ - private readonly ILogger _logger; - - public UnifiedApiResultEndpointFilter(ILogger logger) - { - _logger = logger; - } - - public async ValueTask InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) - { - // 调用下一个过滤器或端点处理程序 - var result = await next(context); - - // 检查返回值类型 - if (result is null) - { - return ResponseOutput.NotOk("No data found."); // 处理 null 返回 - } - - // 如果返回的是元组 - if (result is ValueTuple tuple) - { - return ResponseOutput.Ok(tuple.Item1, tuple.Item2); - } - - if (result is IResponseOutput) - { - return result; - } - - // 对于其他情况,直接返回结果 - return ResponseOutput.Ok(result); - } - - private IResponseOutput WrapResponse(object? value) - { - // 处理元组的情况 - if (value is ValueTuple tuple) - { - return ResponseOutput.Ok(tuple.Item1, tuple.Item2); - } - - // 包装单个对象 - return ResponseOutput.Ok(value); - } -} - -#endregion - diff --git a/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/UnifiedApiResultEndpointFilter.cs b/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/UnifiedApiResultEndpointFilter.cs new file mode 100644 index 000000000..559c9f3ab --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/UnifiedApiResultEndpointFilter.cs @@ -0,0 +1,62 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Service.BusinessFilter; + +#region minimalapi 流程 + + +public class UnifiedApiResultEndpointFilter : IEndpointFilter +{ + private readonly ILogger _logger; + + public UnifiedApiResultEndpointFilter(ILogger logger) + { + _logger = logger; + } + + public async ValueTask InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) + { + // 调用下一个过滤器或端点处理程序 + var result = await next(context); + + // 检查返回值类型 + if (result is null) + { + return ResponseOutput.NotOk("No data found."); // 处理 null 返回 + } + + // 如果返回的是元组 + if (result is ValueTuple tuple) + { + return ResponseOutput.Ok(tuple.Item1, tuple.Item2); + } + + if (result is IResponseOutput) + { + return result; + } + + // 对于其他情况,直接返回结果 + return ResponseOutput.Ok(result); + } + + private IResponseOutput WrapResponse(object? value) + { + // 处理元组的情况 + if (value is ValueTuple tuple) + { + return ResponseOutput.Ok(tuple.Item1, tuple.Item2); + } + + // 包装单个对象 + return ResponseOutput.Ok(value); + } +} + +#endregion \ No newline at end of file diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 9629d60bb..9924157c4 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -12681,6 +12681,13 @@ 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 + + + minimal api 测试 + 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html + 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 + + 清理一致性分析任务 diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 662e23223..0ed9d9fb7 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -40,16 +40,10 @@ namespace IRaCIS.Core.Application.Service /// [ApiExplorerSettings(GroupName = "Institution")] - public class TestMinimalApiService : ServiceBase + public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase { - public TestMinimalApiService() - { - RouteHandlerBuilder = t => - { - t.RequireAuthorization().WithGroupName("Institution").AddEndpointFilter(); - }; - } + public Task> GetProjectList1Async() { @@ -61,6 +55,11 @@ namespace IRaCIS.Core.Application.Service }; return Task.FromResult(list); } + + public IResponseOutput GetTest() + { + return ResponseOutput.Ok(_userInfo.IP); + } }