[英]MaxLength decorator is not working with int.MaxValue
Given this basic GET method:鉴于这个基本的 GET 方法:
public Foo Get([FromQuery][MaxLength(int.MaxValue, ErrorMessage ="Custom error message")] int input)
{
// DO THINGS
}
The MaxLength
decorator is not working when I actually send a value bigger than int.MaxValue
:当我实际发送一个大于
int.MaxValue
的值时, MaxLength
装饰器不起作用:
https://apiurl/foo?input=9999999999999999999
I get a System.OverflowException: 'Value was either too large or too small for an Int32.'
我得到一个
System.OverflowException: 'Value was either too large or too small for an Int32.'
, way before it reaches the MaxLength validator, in System.Private.CoreLib
,在到达 MaxLength 验证器之前,在
System.Private.CoreLib
Is there an easy way to handle this error and actually return my custom Error Message or do I have to implement a custom ModelBinder?是否有一种简单的方法来处理此错误并实际返回我的自定义错误消息,或者我是否必须实现自定义 ModelBinder?
Create an custom attribute:创建自定义属性:
public class CustomNameAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
if (context.Result is BadRequestObjectResult badRequestObjectResult)
if (badRequestObjectResult.Value is ValidationProblemDetails)
{
context.Result = new BadRequestObjectResult("Error message");
}
base.OnResultExecuting(context);
}
}
Then add it to your method:然后将其添加到您的方法中:
[CustomName]
public Foo Get([FromQuery]int input)
{
// DO THINGS
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.