♻️ feat(修改): 调整项目结构
This commit is contained in:
parent
9643be09c0
commit
18e6c9adbf
|
@ -3,7 +3,7 @@ using Bunny.Dao.Common.Result;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Bunny.Common.Filter;
|
||||
namespace Bunny.Common.Attribute;
|
||||
|
||||
/// <summary>
|
||||
/// 全局请求表单验证拦截器
|
|
@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Http;
|
|||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Bunny.Service.Filter;
|
||||
namespace Bunny.Common.Filter;
|
||||
|
||||
/// <summary>
|
||||
/// 自定义权限过滤器
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using System.Net;
|
||||
using Bunny.Dao.Common.Constant.Result;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bunny.Dao.Common.Result;
|
||||
|
||||
|
@ -30,12 +29,6 @@ public class Result<T>(HttpStatusCode code, T? data, string? message)
|
|||
/// </summary>
|
||||
public T? Data { get; set; } = data;
|
||||
|
||||
// 不再继承 IActionResult
|
||||
public Task ExecuteResultAsync(ActionContext context)
|
||||
{
|
||||
return new Task<Result<T>>(() => new Result<T>(code, data, message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 操作成功
|
||||
/// </summary>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Bunny.Common.Filter;
|
||||
using Bunny.Service.Filter;
|
||||
using Bunny.Common.Attribute;
|
||||
using Bunny.Common.Filter;
|
||||
using Bunny.Service.Job.JobService;
|
||||
using Lazy.Captcha.Core;
|
||||
using Lazy.Captcha.Core.Generator;
|
||||
|
|
|
@ -329,3 +329,46 @@ public class JsonDateTimeConverter : JsonConverter<DateTime>
|
|||
}
|
||||
```
|
||||
|
||||
## 常见过滤器
|
||||
|
||||
Authorization Filter(授权过滤器):用于控制用户是否有权限访问特定的资源。
|
||||
|
||||
Action Filter(动作过滤器):在执行动作方法前后执行特定的逻辑。
|
||||
|
||||
Result Filter(结果过滤器):在执行结果前后执行特定的逻辑。
|
||||
|
||||
Exception Filter(异常过滤器):用于处理应用程序中发生的异常。
|
||||
|
||||
1. AuthorizationFilterAttribute
|
||||
是所有过滤器中最先执行的。
|
||||
用于执行授权检查。
|
||||
只有一个方法:OnAuthorization,它在ActionFilterAttribute的OnActionExecuting之前执行。
|
||||
如果授权失败,可以抛出AuthorizationException或返回Challenge或Forbid结果来阻止请求继续执行。
|
||||
|
||||
2. ResourceFilterAttribute
|
||||
用于在动作方法执行前后立即执行逻辑,但比ActionFilterAttribute更早或更晚。
|
||||
有两个方法:OnResourceExecuting和OnResourceExecuted。
|
||||
可以用来执行需要在动作方法执行前或后立即进行的操作,例如缓存。
|
||||
|
||||
3. ExceptionFilterAttribute
|
||||
专门用于处理异常。
|
||||
有两个方法:OnException和OnExceptionAsync。
|
||||
通常用于捕获和处理动作方法执行过程中抛出的异常。
|
||||
由于它在异常发生后才执行,因此不适合用于清理资源,因为那时资源可能已经被破坏。
|
||||
|
||||
4. ResultFilterAttribute
|
||||
用于在动作结果执行前后执行逻辑。
|
||||
有两个方法:OnResultExecuting和OnResultExecuted。
|
||||
与ActionFilterAttribute类似,但专注于动作结果的处理,例如渲染视图或返回JSON数据。
|
||||
|
||||
5. FormatFilterAttribute
|
||||
是一个内置的过滤器,用于处理内容协商。
|
||||
自动添加到控制器或动作方法上,当请求指定媒体类型时触发。
|
||||
|
||||
6. ServiceFilterAttribute
|
||||
用于从服务容器中解析并执行一个过滤器实例。
|
||||
不是直接实现过滤器逻辑,而是引用一个已经注册到依赖注入容器中的过滤器实例。
|
||||
|
||||
7. TypeFilterAttribute
|
||||
类似于ServiceFilterAttribute,但它是通过反射创建过滤器实例,不需要过滤器在DI容器中注册。
|
||||
可以接受参数,用于构造过滤器实例。
|
Loading…
Reference in New Issue