Why is ValidateInput(False) not working?

前端 未结 7 2396
无人共我
无人共我 2020-11-29 02:21

I am converting an application I created using webforms to the asp.net mvc framework using vb.net. I have a problem with one of my views. I get the yellow screen of death sa

7条回答
  •  Happy的楠姐
    2020-11-29 02:59

    When you are using your own model binders which implement the IModelBinder interface you will notice that those custom model binders always validate the data, regardless any attributes. You can add few lines of code to make the custom model binders respect the ValidateInput filter of the actions:

    // First check if request validation is required
    var shouldPerformRequestValidation = controllerContext.Controller.ValidateRequest && bindingContext.ModelMetadata.RequestValidationEnabled;
    
    // Get value
    var valueProviderResult = bindingContext.GetValueFromValueProvider(shouldPerformRequestValidation);
    if (valueProviderResult != null)
    {
        var theValue = valueProviderResult.AttemptedValue;
    
        // etc...
    }
    

    This is explained very nicely by Martijn Boland here: http://blogs.taiga.nl/martijn/2011/09/29/custom-model-binders-and-request-validation/

提交回复
热议问题