asp.net-mvcmodel-bindingmodelbindersvalue-provider

Use custom ASP.NET MVC IValueProvider, without setting it globally?


I want to be able to grab keys/values from a cookie and use that to bind a model.

Rather than building a custom ModelBinder, I believe that the DefaultModelBinder works well out of the box, and the best way to choose where the values come from would be to set the IValueProvider that it uses.

To do this I don't want to create a custom ValueProviderFactory and bind it globally, because I only want this ValueProvider to be used in a specific action method.

I've built an attribute that does this:

/// <summary>
/// Replaces the current value provider with the specified value provider
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class SetValueProviderAttribute : ActionFilterAttribute
{
    public SetValueProviderAttribute(Type valueProviderType)
    {
        if (valueProviderType.GetInterface(typeof(IValueProvider).Name) == null)
            throw new ArgumentException("Type " + valueProviderType + " must implement interface IValueProvider.", "valueProviderType");

        _ValueProviderType = valueProviderType;
    }

    private Type _ValueProviderType;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        IValueProvider valueProviderToAdd = GetValueProviderToAdd();

        filterContext.Controller.ValueProvider = valueProviderToAdd;
    }

    private IValueProvider GetValueProviderToAdd()
    {
        return (IValueProvider)Activator.CreateInstance(_ValueProviderType);
    }
}

Unfortunately, the ModelBinder and its IValueProvider are set BEFORE OnActionExecuting (why?????). Has anyone else figured out a way to inject a custom IValueProvider into the DefaultModelBinder without using the ValueProviderFactory?


Solution

  • Figured out how to do this. First, create a custom model binder that takes a value provider type in the constructor - but inherits from default modelbinder. This allows you to use standard model binding with a custom value provider:

    /// <summary>
    /// Uses default model binding, but sets the value provider it uses
    /// </summary>
    public class SetValueProviderDefaultModelBinder : DefaultModelBinder
    {
        private Type _ValueProviderType;
    
        public SetValueProviderDefaultModelBinder(Type valueProviderType)
        {
            if (valueProviderType.GetInterface(typeof(IValueProvider).Name) == null)
                throw new ArgumentException("Type " + valueProviderType + " must implement interface IValueProvider.", "valueProviderType");
    
            _ValueProviderType = valueProviderType;
        }
    
        /// <summary>
        /// Before binding the model, set the IValueProvider it uses
        /// </summary>
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            bindingContext.ValueProvider = GetValueProvider();
    
            return base.BindModel(controllerContext, bindingContext);
        }
    
        private IValueProvider GetValueProvider()
        {
            return (IValueProvider)Activator.CreateInstance(_ValueProviderType);
        }
    }
    

    Then we create a model binding attribute that will inject the value provider type in the custom model binder created above, and use that as the model binder:

    /// <summary>
    /// On the default model binder, replaces the current value provider with the specified value provider.  Cannot use custom model binder with this.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
    public class SetValueProviderAttribute : CustomModelBinderAttribute
    {
        // Originally, this was an action filter, that OnActionExecuting, set the controller's IValueProvider, expecting it to be picked up by the default model binder
        // when binding the model.  Unfortunately, OnActionExecuting occurs AFTER the IValueProvider is set on the DefaultModelBinder.  The only way around this is
        // to create a custom model binder that inherits from DefaultModelBinder, and in its BindModel method set the ValueProvider and then do the standard model binding.
    
        public SetValueProviderAttribute(Type valueProviderType)
        {
            if (valueProviderType.GetInterface(typeof(IValueProvider).Name) == null)
                throw new ArgumentException("Type " + valueProviderType + " must implement interface IValueProvider.", "valueProviderType");
    
            _ValueProviderType = valueProviderType;
        }
    
        private Type _ValueProviderType;
    
        public override IModelBinder GetBinder()
        {
            var modelBinder = new SetValueProviderDefaultModelBinder(_ValueProviderType);
            return modelBinder;
        }
    }