简体   繁体   English

禁用自定义活页夹的 model 验证

[英]Disable model validation for custom binder

I have created a custom model binder and would like to disable model validation.我创建了一个自定义 model 活页夹,并想禁用 model 验证。 How to do that?怎么做?

   public class ConverterModelBinder : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            ///...creating and binding ConversionModel 
            bindingContext.Result = ModelBindingResult.Success(ConversionModel );

            return Task.CompletedTask;
        }
}

The controller action is never executed because build in model validator kicks in for my ConversionModel object. controller 操作永远不会执行,因为在 model 验证器中构建了我的 ConversionModel object。 I don't want to validate it.我不想验证它。

public async Task<ActionResult> Convert([ModelBinder(typeof(ConverterModelBinder))] ConversionModel model, CancellationToken cancellationToken)
        {
        }

You can try to use the following methods to disable model validation:您可以尝试使用以下方法禁用 model 验证:

Method 1: Use the ValidateNever attribute:方法 1:使用ValidateNever属性:

[ValidateNever]
public class ConversionModel {
   //... properties
}

Method 2: Set the ApiBehaviorOptions.SuppressModelStateInvalidFilter Property to true :方法 2:将ApiBehaviorOptions.SuppressModelStateInvalidFilter 属性设置为true

builder.Services.Configure<ApiBehaviorOptions>(options =>
{
    options.SuppressModelStateInvalidFilter = true;
});

Refer to this thread: Correct way to disable model validation in ASP.Net Core 2 MVC请参阅此线程: 在 ASP.Net Core 2 MVC 中禁用 model 验证的正确方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM