diff --git a/Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs b/Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs index 729c2fb9..c1d4a6b9 100644 --- a/Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs +++ b/Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs @@ -140,7 +140,7 @@ public FromBodyRequest() /// public class CustomApiRequestBinder : IReqBinder { - public static ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) + public static ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) { var request = new HelloApiRequest(); diff --git a/Biwen.QuickApi.DemoWeb/Program.cs b/Biwen.QuickApi.DemoWeb/Program.cs index 67acc39f..163cf8ff 100644 --- a/Biwen.QuickApi.DemoWeb/Program.cs +++ b/Biwen.QuickApi.DemoWeb/Program.cs @@ -226,7 +226,7 @@ //app.MapGenQuickApis(app.Services); //测试其他地方调用QuickApi -app.MapGet("/fromapi", async Task, BadRequest>>> +app.MapGet("/fromapi", async Task, ValidationProblem>> (JustAsService api) => { @@ -236,7 +236,7 @@ var result = req.Validate(); if (!result.IsValid) { - return TypedResults.BadRequest(result.ToDictionary()); + return TypedResults.ValidationProblem(result.ToDictionary()); } //执行请求 @@ -296,7 +296,7 @@ public partial class BindRequest : BaseRequest, IReqBinder BindAsync(HttpContext context, ParameterInfo parameter = null!) + public static async ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) { //返回默认绑定 var req = await DefaultReqBinder.BindAsync(context, parameter); diff --git a/Biwen.QuickApi/Abstractions/IReqBinder.cs b/Biwen.QuickApi/Abstractions/IReqBinder.cs index 6909be8e..026823a9 100644 --- a/Biwen.QuickApi/Abstractions/IReqBinder.cs +++ b/Biwen.QuickApi/Abstractions/IReqBinder.cs @@ -10,6 +10,6 @@ namespace Biwen.QuickApi.Abstractions /// public interface IReqBinder where T : class, new() { - static abstract ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!); + static abstract ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!); } } \ No newline at end of file diff --git a/Biwen.QuickApi/Biwen.QuickApi.csproj b/Biwen.QuickApi/Biwen.QuickApi.csproj index 96106d1c..50dbf0cf 100644 --- a/Biwen.QuickApi/Biwen.QuickApi.csproj +++ b/Biwen.QuickApi/Biwen.QuickApi.csproj @@ -5,7 +5,7 @@ latest enable true - 1.6.0 + 1.6.1 net8.0 https://github.com/vipwan/Biwen.QuickApi 万雅虎 diff --git a/Biwen.QuickApi/DefaultReqBinder.cs b/Biwen.QuickApi/DefaultReqBinder.cs index b29c4668..af072174 100644 --- a/Biwen.QuickApi/DefaultReqBinder.cs +++ b/Biwen.QuickApi/DefaultReqBinder.cs @@ -20,7 +20,7 @@ namespace Biwen.QuickApi /// /// /// - public static async ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) + public static async ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) { IDictionary? ReadFromJsonDic = null; @@ -278,9 +278,9 @@ namespace Biwen.QuickApi /// internal sealed class EmptyReqBinder : IReqBinder where T : class, new() { - public static ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) + public static ValueTask BindAsync(HttpContext context, ParameterInfo parameter = null!) { - return ValueTask.FromResult(default(T)); + return ValueTask.FromResult(new T()); } } diff --git a/Biwen.QuickApi/ServiceRegistration.cs b/Biwen.QuickApi/ServiceRegistration.cs index 4e3340f1..0abc7967 100644 --- a/Biwen.QuickApi/ServiceRegistration.cs +++ b/Biwen.QuickApi/ServiceRegistration.cs @@ -401,11 +401,13 @@ async static Task RequestHandler(IHttpContextAccessor ctx, Type apiType //使用接口静态成员重写代码: MethodInfo methodInfo = (((dynamic)api).ReqBinder).GetMethod("BindAsync", BindingFlags.Static | BindingFlags.Public); var req = await (dynamic)methodInfo.Invoke(null, [ctx.HttpContext, null])!; - - //验证DTO - if (req.Validate() is ValidationResult { IsValid: false } vresult) + if (req is not null) { - return TypedResults.ValidationProblem(vresult.ToDictionary()); + //验证DTO + if (req.Validate() is ValidationResult { IsValid: false } vresult) + { + return TypedResults.ValidationProblem(vresult.ToDictionary()); + } } var result = await ((dynamic)api)!.ExecuteAsync(req!); var rawResult = InnerResult(result);