diff --git a/Communications/SimApiBaseResponse.cs b/Communications/SimApiBaseResponse.cs index 2c7a01a..ea0ff66 100644 --- a/Communications/SimApiBaseResponse.cs +++ b/Communications/SimApiBaseResponse.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; +using SimApi.Helpers; namespace SimApi.Communications; @@ -36,15 +37,7 @@ public class SimApiBaseResponse(int code = 200, string message = "成功") /// public override string ToString() { - return JsonSerializer.Serialize(this, new JsonSerializerOptions - { - IgnoreReadOnlyProperties = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - Converters = - { - new JsonStringEnumConverter() - } - }); + return SimApiUtil.Json(this); } } diff --git a/Helpers/SimApiResponseFilter.cs b/Helpers/SimApiResponseFilter.cs new file mode 100644 index 0000000..2737b75 --- /dev/null +++ b/Helpers/SimApiResponseFilter.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using SimApi.Communications; + +namespace SimApi.Helpers; + +public class SimApiResponseFilter : IResultFilter +{ + public void OnResultExecuting(ResultExecutingContext context) + { + context.Result = context.Result switch + { + // 检查结果是否为 null + null => new OkObjectResult(new SimApiBaseResponse()), + ObjectResult { Value: SimApiBaseResponse simApiBaseResponse } => + new OkObjectResult(simApiBaseResponse), + ObjectResult objectResult => new OkObjectResult(new SimApiBaseResponse(objectResult.Value!)), + EmptyResult => new OkObjectResult(new SimApiBaseResponse()), + _ => context.Result + }; + } + + public void OnResultExecuted(ResultExecutedContext context) + { + } +} \ No newline at end of file diff --git a/Middlewares/SimApiExceptionMiddleware.cs b/Middlewares/SimApiExceptionMiddleware.cs index 30099f4..7814136 100644 --- a/Middlewares/SimApiExceptionMiddleware.cs +++ b/Middlewares/SimApiExceptionMiddleware.cs @@ -1,5 +1,5 @@ using System; -using System.Linq; +using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using SimApi.Communications; @@ -27,12 +27,11 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger private static void ErrorResponse(HttpContext context, SimApiBaseResponse response) { - if (context.Response.HasStarted) return; context.Response.StatusCode = 200; context.Response.Headers.Append("Content-Type", "application/json"); - context.Response.WriteAsync(response.ToString()); + context.Response.WriteAsync(response.ToString()).Wait(); } } \ No newline at end of file diff --git a/SimApi.csproj b/SimApi.csproj index 5a2b317..8e004ac 100644 --- a/SimApi.csproj +++ b/SimApi.csproj @@ -19,9 +19,7 @@ - - diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 888b762..13a1af6 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Text.Json; using SimApi.Helpers; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; @@ -209,6 +210,12 @@ public static class SimApiExtensions builder.AddSingleton(); } + if (simApiOptions.EnableSimApiException) + { + builder.AddControllers(opt => opt.Filters.Add()) + .AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase); + } + builder.AddSingleton(simApiOptions); return builder; }