开发者

ValidateInput(false) still trigger the validation in IValidatableObject

开发者 https://www.devze.com 2023-04-11 11:14 出处:网络
I have a controller which I dont want to validate, when called upon. My controller: [Authorize(Roles = \"Admin\")]

I have a controller which I dont want to validate, when called upon.

My controller:

[Authorize(Roles = "Admin")]
[HttpPost]
[ValidateInput(false)]
public ActionResult Delete(MyLINQClass model)
{
    // Do something
}

My model:

[MetadataType(typeof(MyLINQClass MetaData))]
public partial class MyLINQClass : DefaultModel, IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(Valid开发者_运维问答ationContext validationContext)
    {
         // Do validation
    }
}

I do not want the validate to be triggered, and I thought adding [ValidateInput(false)] would help. But the Validate() is still triggered.

Im using ASP MVC 3 and .NET 4.


The [ValidateInput(false)] is not related to model validation. It disables ASP.NET validation for XSS characters in the request such as <, >, ... The validation is triggered by the default model binder when it tries to bind the MyViewModel parameters. If you don't want to perform validation, simply write another view model that the Delete action will take as parameter and which won't have any Validate method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号