asp.net mvc : modify some value that comes from view before it reaches the action method
The thing is that all my ids are encrypted and I have 2 methods that parses the Id from long to the Encrypted (something like A8sdf=dsfs=) and back so I have to do this conversion in each action 开发者_StackOverflowmethod when I send or receive an EncryptedId
Is it possible to modify the value for a specific type or Property Name before it reaches the action method ?
You could write a model binder that will decrypt the value:
public class MyModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
{
// Test if it is the Id property
if (propertyDescriptor.Name == "Id")
{
// Remark: MyDecryptFunction must return the same type
value = MyDecryptFunction(value);
}
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
}
精彩评论