处理自动模型验证

问题描述 投票:1回答:1

我有一个问题,每当我输入一个整数和布尔数据类型的字符串请求。我有一个奇怪的错误消息,由NET CORE2.1新功能自动模型验证引起。

这是请求

{   
    "EmailAddress" : "[email protected]",
    "EmailAddressConfirm" : "[email protected]",
    "FirstName" : "KL",
    "MiddleName": "M",
    "LastName" : "Lawingco",
    "InquiryTypeID": "asda" ,
    "InquiryContent" : "test",
    "Validation" : true,
    "TermsAndCondition" : false
}

它会抛出这样的错误

无法将字符串转换为整数:asda。路径'InquiryTypeID',第7行,第26位。

请注意,我也使用FluentValidation,但自2.1以来,引入了自动模型验证。我的问题是,只要给int或boolean数据类型输入错误,我就可以修改错误消息

我尝试使用此功能关闭自动模型验证

services.Configure<ApiBehaviorOptions>(opt =>
{
    opt.SuppressModelStateInvalidFilter = true;
});

但它也杀了我的FluentValidation

这是我的模特

public class NewInquiry
{
    public string EmailAddress{ get; set; }

    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    public string LastName { get; set; }

    public string EmailAddressConfirm { get; set; }

    public int? InquiryTypeID { get; set; }

    public string InquiryContent { get; set; }

    public bool Validation { get; set; }

    public bool TermsAndCondition { get; set; }

}

希望你能帮帮我,谢谢

c# asp.net-core asp.net-core-2.1
1个回答
0
投票

是的,您可以自定义/本地化所有模型输入ModelBinding错误消息。

在您的启动文件中,您可以访问ModelBindingMessageProvider并设置所有模型绑定消息:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddMvcOptions(o =>
    {
        o.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor((x, y) =>
            string.Format("The value '{0}' is not valid for '{1}'", x, y));

        o.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor((x) => 
            string.Format("A value for the '{0}' property was not provided", x));

        o.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor(() => 
            "A value is required");

        o.ModelBindingMessageProvider.SetMissingRequestBodyRequiredValueAccessor(() => 
            "A non-empty request body is required");

        o.ModelBindingMessageProvider.SetNonPropertyAttemptedValueIsInvalidAccessor((x) => 
            string.Format("The value '{0}' is not valid", x));

        o.ModelBindingMessageProvider.SetNonPropertyUnknownValueIsInvalidAccessor(() => 
            "The supplied value is invalid");

        o.ModelBindingMessageProvider.SetNonPropertyValueMustBeANumberAccessor(() => 
            "The field must be a number");

        o.ModelBindingMessageProvider.SetUnknownValueIsInvalidAccessor((x) => 
            string.Format("The supplied value is invalid for {0}", x));

        o.ModelBindingMessageProvider.SetValueIsInvalidAccessor((x) => 
            string.Format("The value '{0}' is invalid", x));

        o.ModelBindingMessageProvider.SetValueMustBeANumberAccessor((x) => 
            string.Format("The field '{0}' must be a number", x));

        o.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor((x) => 
            string.Format("The field '{0}' must not be null", x));
    });

同样的方法也可用于本地化

© www.soinside.com 2019 - 2024. All rights reserved.