Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NETCore3.1 WebAPI中,进行JSON请求验证最合理的地方是什么?_C#_Asp.net Core_.net Core_Asp.net Core Webapi_Asp.net Core 3.1 - Fatal编程技术网

C# 在ASP.NETCore3.1 WebAPI中,进行JSON请求验证最合理的地方是什么?

C# 在ASP.NETCore3.1 WebAPI中,进行JSON请求验证最合理的地方是什么?,c#,asp.net-core,.net-core,asp.net-core-webapi,asp.net-core-3.1,C#,Asp.net Core,.net Core,Asp.net Core Webapi,Asp.net Core 3.1,我正在编写一些.NET core 3.1 WebAPI,尽管模型验证(通过ValidationAttribute)负责我的大部分请求验证,但请求验证并不尊重DataTypeAttribute 例如,如果我的端点上有一个十进制的属性,并且有人提供了一个字符串,那么它在请求验证时失败,并且我的控制器端点没有被命中 我知道有一些替代方案,我只是想知道将请求验证逻辑放在哪里最有意义 示例:ValidationAttribute(不会为请求验证而激发)、ActionFilters(OnActionExec

我正在编写一些.NET core 3.1 WebAPI,尽管模型验证(通过ValidationAttribute)负责我的大部分请求验证,但请求验证并不尊重DataTypeAttribute

例如,如果我的端点上有一个十进制的属性,并且有人提供了一个字符串,那么它在请求验证时失败,并且我的控制器端点没有被命中

我知道有一些替代方案,我只是想知道将请求验证逻辑放在哪里最有意义

示例:ValidationAttribute(不会为请求验证而激发)、ActionFilters(OnActionExecuting,它会激发)、JsonInputFormatters(也会激发)等等

==========编辑:==========

谢谢你的回答。我现在正在从ActionFilterAttribute创建一个子类CustomActionFilterAttribute。然后我用这个filter属性装饰我的控制器及其操作,但是OnActionExecuting没有启动

我已尝试(在Startup.ConfigureServices()中)执行以下操作:


MyController中的任何操作都不会触发CustomActionFilterAttribute的OnActionExecuting函数。我在这里遗漏了什么吗?

DataTypeAttribute没有用于验证,但我仍然认为这个问题属于模型验证的范畴。你不能在这个属性上添加一个自定义验证器吗?e、 g.(未经测试):


DataTypeAttribute没有用于验证,但我仍然认为这个问题属于模型验证的范畴。你不能在这个属性上添加一个自定义验证器吗?e、 g.(未经测试):

HttpMethods中的示例

[HttpGet("{id:int}/{year:min(2000)}/{month:range(1,2)})"
或验证筛选器

 public class Validation : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
               //This is my custom Error Model
                ErrorDTO error = new ErrorDTO();
                error.status = 400;

                var ModelErrors = context.ModelState.Keys.ToList();
                var Errors = context.ModelState.Values.SelectMany(x => x.Errors).ToList();

                for (int i = 0; i < Errors.Count; i++)
                {
                    error.Errors.Add($"{ModelErrors[i]} {Errors[i].ErrorMessage}");
                }

               
                context.Result = new BadRequestObjectResult(error);


            }
        }
    }
公共类验证:ActionFilterAttribute
{
公共重写无效OnActionExecuting(ActionExecutingContext上下文)
{
如果(!context.ModelState.IsValid)
{
//这是我的自定义错误模型
ErrorDTO error=新ErrorDTO();
error.status=400;
var ModelErrors=context.ModelState.Keys.ToList();
var Errors=context.ModelState.Values.SelectMany(x=>x.Errors.ToList();
for(int i=0;i
HttpMethods中的示例

[HttpGet("{id:int}/{year:min(2000)}/{month:range(1,2)})"
或验证筛选器

 public class Validation : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
               //This is my custom Error Model
                ErrorDTO error = new ErrorDTO();
                error.status = 400;

                var ModelErrors = context.ModelState.Keys.ToList();
                var Errors = context.ModelState.Values.SelectMany(x => x.Errors).ToList();

                for (int i = 0; i < Errors.Count; i++)
                {
                    error.Errors.Add($"{ModelErrors[i]} {Errors[i].ErrorMessage}");
                }

               
                context.Result = new BadRequestObjectResult(error);


            }
        }
    }
公共类验证:ActionFilterAttribute
{
公共重写无效OnActionExecuting(ActionExecutingContext上下文)
{
如果(!context.ModelState.IsValid)
{
//这是我的自定义错误模型
ErrorDTO error=新ErrorDTO();
error.status=400;
var ModelErrors=context.ModelState.Keys.ToList();
var Errors=context.ModelState.Values.SelectMany(x=>x.Errors.ToList();
for(int i=0;i
不幸的是,我的ValidationAttribute只有在请求被验证后才点击“IsValid”。这反过来会从JSON序列化程序返回“无法将字段转换为十进制”的一般消息理想情况下,我希望在请求点截获验证。不幸的是,我的ValidationAttribute仅在验证请求后点击“IsValid”。这反过来会从JSON序列化程序返回“无法将字段转换为十进制”的一般消息。理想情况下,我希望在请求点截获验证。谢谢,这很有效。非常感谢。
 public class Validation : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
               //This is my custom Error Model
                ErrorDTO error = new ErrorDTO();
                error.status = 400;

                var ModelErrors = context.ModelState.Keys.ToList();
                var Errors = context.ModelState.Values.SelectMany(x => x.Errors).ToList();

                for (int i = 0; i < Errors.Count; i++)
                {
                    error.Errors.Add($"{ModelErrors[i]} {Errors[i].ErrorMessage}");
                }

               
                context.Result = new BadRequestObjectResult(error);


            }
        }
    }