Encrypt Route Data in URL

后端 未结 2 1362
孤独总比滥情好
孤独总比滥情好 2021-01-03 14:49

In my ASP.NET MVC app, I want to encrypt the route data and NOT QueryString, in other word:

I\'m using the ASP.NET MVC default route pattern :

   rou         


        
2条回答
  •  旧时难觅i
    2021-01-03 15:23

    You can use a custom model binder for this parameter

    // /Controller/Example/0000000A
    public ActionResult Example([ModelBinder(typeof(EncryptDataBinder))]int id)
    {
        return View(id);
    }
    

    The model binder

    public class EncryptDataBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(int))
            {
                var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                if (valueProviderResult != null)
                {
                    // Use your own logic here
                    bytes = ConvertUtilities.ToBytesFromHexa((string)valueProviderResult.RawValue);
                    return BitConverter.ToInt32(bytes, 0);
                }
            }
    
            return base.BindModel(controllerContext, bindingContext);
        }
    }
    

提交回复
热议问题