add filter

This commit is contained in:
2024-12-23 20:26:21 +08:00
parent e27d5e1c77
commit 404a638d5e
5 changed files with 40 additions and 18 deletions
+2 -9
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using SimApi.Helpers;
namespace SimApi.Communications; namespace SimApi.Communications;
@@ -36,15 +37,7 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
/// <returns></returns> /// <returns></returns>
public override string ToString() public override string ToString()
{ {
return JsonSerializer.Serialize(this, new JsonSerializerOptions return SimApiUtil.Json(this);
{
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
}
});
} }
} }
+27
View File
@@ -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<object>(objectResult.Value!)),
EmptyResult => new OkObjectResult(new SimApiBaseResponse()),
_ => context.Result
};
}
public void OnResultExecuted(ResultExecutedContext context)
{
}
}
+4 -7
View File
@@ -1,5 +1,5 @@
using System; using System;
using System.Linq; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using SimApi.Communications; using SimApi.Communications;
@@ -27,12 +27,11 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
switch (context.Response.StatusCode) switch (context.Response.StatusCode)
{ {
case 200: case 200:
break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
case 301: case 301:
case 302: case 302:
break; break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
default: default:
throw new SimApiException(context.Response.StatusCode); throw new SimApiException(context.Response.StatusCode);
} }
@@ -42,7 +41,6 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
response = string.IsNullOrEmpty(ex.Message) response = string.IsNullOrEmpty(ex.Message)
? new SimApiBaseResponse(ex.Code) ? new SimApiBaseResponse(ex.Code)
: new SimApiBaseResponse(ex.Code, ex.Message); : new SimApiBaseResponse(ex.Code, ex.Message);
ErrorResponse(context, response); ErrorResponse(context, response);
} }
catch (Exception ex) catch (Exception ex)
@@ -61,9 +59,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
/// <param name="response"></param> /// <param name="response"></param>
private static void ErrorResponse(HttpContext context, SimApiBaseResponse response) private static void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{ {
if (context.Response.HasStarted) return;
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.Headers.Append("Content-Type", "application/json"); context.Response.Headers.Append("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString()); context.Response.WriteAsync(response.ToString()).Wait();
} }
} }
-2
View File
@@ -19,9 +19,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Helpers\"/>
<Folder Include="Communications\"/> <Folder Include="Communications\"/>
<Folder Include="Middlewares\"/>
<Folder Include="Exceptions\"/> <Folder Include="Exceptions\"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+7
View File
@@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json;
using SimApi.Helpers; using SimApi.Helpers;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -209,6 +210,12 @@ public static class SimApiExtensions
builder.AddSingleton<SimApiStorage>(); builder.AddSingleton<SimApiStorage>();
} }
if (simApiOptions.EnableSimApiException)
{
builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>())
.AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
}
builder.AddSingleton(simApiOptions); builder.AddSingleton(simApiOptions);
return builder; return builder;
} }