我正在编写一些 .NET core 3.1 WebAPI,虽然模型验证(通过 ValidationAttributes)负责我的大部分请求验证,但请求验证不尊重 DataTypeAttribute。
因此,例如,如果我的端点上有一个小数点属性并且有人提供了一个字符串,那么它在请求验证时会失败,并且我的控制器端点不会被命中。
我知道有几种选择,我只是想知道将请求验证逻辑放在哪里最有意义?
示例:ValidationAttribute(不会触发请求验证)、ActionFilters(OnActionExecuting,会触发)、JsonInputFormatters(也会触发)等...
========== 编辑:==========
谢谢你的回答。我现在正在从 ActionFilterAttribute 创建一个子类 CustomActionFilterAttribute。然后我用这个过滤器属性装饰我的控制器及其动作,但 OnActionExecuting 没有触发。
我尝试过(在 Startup.ConfigureServices() 中):
services.AddScoped\<CustomActionFilterAttribute>();
services.AddControllers(
config =>
{
config.Filters.Add\<CustomActionFilterAttribute>();
});
然后我像这样装饰我的控制器:
[ValidateSubUsersModelFilter]
public class MyController : Controller {}
MyController 中的所有操作都不会触发 CustomActionFilterAttribute 的 OnActionExecuting 函数。我在这里错过了什么吗?