Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:06:22+00:00 2026-05-27T22:06:22+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:06:22+00:00Added an answer on May 27, 2026 at 10:06 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to grab the Title of a web page from
I want to be able to grab content from web pages, especially the tags
I want to be able to occasionally programmatically grab some data from the graph
I want to be able to grab the links from an rss feed and
I want to be able to run netstat -n and grab the output somehow
I want to be able to bind an event to the axes in high
I want to be able to capture the exception that is thrown when a
I want to grab a form object from an external javascript function instead of
Basically I want to be able to grab the ending of an url, and
I'm building an app that needs to be able to extend facebook graph data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.