jeudi 13 août 2015

OnPropertyValidating method for CustomModelBinder never gets called

Hi I am new to asp net mvc programming and I am wondering why the OnPropertyValidating method for my CustomModelBinder class is not being called.

Here is my declaration for the CUstomModelBinder.

public class AnotherModelBinder : DefaultModelBinder
{
    protected override bool OnPropertyValidating(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
    {
        if (value is string && (controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)))
        {
            if (controllerContext.Controller.ValidateRequest && bindingContext.PropertyMetadata[propertyDescriptor.Name].RequestValidationEnabled)
            {
                int index;
                if (IsDangerousString(value.ToString(), out index))
                {
                    throw new HttpRequestValidationException("Dangerous Input Detected");
                }
            }               
        }
        return base.OnPropertyValidating(controllerContext, bindingContext, propertyDescriptor, value);
    }

}

and here is what I added to the Global.asax

    ModelBinders.Binders.Add(typeof(AnotherModelBinder), new AnotherModelBinder());
    ModelBinders.Binders.DefaultBinder = new AnotherModelBinder();

now I am assuming that this OnPropertyValidating method will get called everytime I call a controller action something like :

[HttpPost]
public JsonResult TestMethod(int param1, string param2, string param3)
{
    ...
}

but the OnPropertyValidating method on my customModelBinder never gets called.

Can anyone help me to understand why? Is there any good tutorial sites for this?

Thank in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire