开发者

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);
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜