diff --git a/Attributes/OriginResponseAttribute.cs b/Attributes/OriginResponseAttribute.cs new file mode 100644 index 0000000..ad6c5fc --- /dev/null +++ b/Attributes/OriginResponseAttribute.cs @@ -0,0 +1,8 @@ +using System; + +namespace SimApi.Attributes; + +[AttributeUsage(AttributeTargets.Method)] +public class OriginResponseAttribute : Attribute +{ +} \ No newline at end of file diff --git a/Configurations/SimApiOptions.cs b/Configurations/SimApiOptions.cs index 6f08cd5..3609ce1 100644 --- a/Configurations/SimApiOptions.cs +++ b/Configurations/SimApiOptions.cs @@ -36,6 +36,11 @@ public class SimApiOptions /// default: true /// public bool EnableSimApiException { get; set; } = true; + + /// + /// 启用返回结果拦截 + /// + public bool EnableSimApiResponseFilter { get; set; } = true; /// /// 开启S3兼容的存储系统。 diff --git a/Helpers/SimApiResponseFilter.cs b/Helpers/SimApiResponseFilter.cs index 2737b75..c0bf7fb 100644 --- a/Helpers/SimApiResponseFilter.cs +++ b/Helpers/SimApiResponseFilter.cs @@ -1,6 +1,8 @@ using System; +using System.Linq; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; +using SimApi.Attributes; using SimApi.Communications; namespace SimApi.Helpers; @@ -9,6 +11,11 @@ public class SimApiResponseFilter : IResultFilter { public void OnResultExecuting(ResultExecutingContext context) { + if (context.ActionDescriptor.EndpointMetadata.Any(meta => meta is OriginResponseAttribute)) + { + return; + } + context.Result = context.Result switch { // 检查结果是否为 null