在我的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參數未在模型中定義 - 它位於默認路線 –