在我的MVC 3解决方案中,我希望将查询字符串中的所有Id都加密。要解密的URL我从DefaultModelBinder继承和overrided BindProperty方法:自定义模型绑定问题
public class CryptedIdBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name.ToLower() == "id")
{
propertyDescriptor.SetValue(bindingContext.Model, CryptoHelper.Decrypt(controllerContext.HttpContext.Request.Form["id"]));
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
return;
}
之后,我设置在Global.asax的新DefaultBinder上的Application_Start:
System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new CryptedIdBinder();
我没有从IModelBinder继承,因为我想更改绑定逻辑只有解决方案中的id字段。
问题是BindProperty方法从不被调用。我错了什么?
PS。为了确保我打电话至少BindModel方法我将我的定制绑定在这个代码的和平,它是由调试器击中:
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return base.BindModel(controllerContext, bindingContext);
}
你可以张贴一些示例模型代码?也许DefaultModelBinder过滤了你的一些属性... – nemesv
对不起,我没有听到你的想法。它不是一个全球活页夹,它应该适用于所有型号?此Id参数未在模型中定义 - 它位于默认路线 –