Passing Dictionary<string, object> to MVC Controller Passing Dictionary<string, object> to MVC Controller json json

Passing Dictionary<string, object> to MVC Controller


It appends two values because by default MVC registers such ValueProviderFactory:

public sealed class RouteDataValueProviderFactory : ValueProviderFactory

That returns implementation of IValueProvider - RouteDataValueProvider:

public sealed class RouteDataValueProvider : DictionaryValueProvider<object>{    // RouteData should use the invariant culture since it's part of the URL, and the URL should be    // interpreted in a uniform fashion regardless of the origin of a particular request.    public RouteDataValueProvider(ControllerContext controllerContext)        : base(controllerContext.RouteData.Values, CultureInfo.InvariantCulture)    {    }}

Basically it just binds Dictionary to route values for current route.

For example if you add such data to routes in RouteConfig:

routes.MapRoute(    name: "Default",    url: "{controller}/{action}",    defaults: new { controller = "Home", action = "Index", SomeSpecificRouteData = 42 });

Then your Dictionary will have 3 values - controller, action and SomeSPecificRouteData.

Another sample is that you can define such action:

public ActionResult Index(string action, string controller, int SomeSpecificRouteData)

and RouteDataValueProvider will pass data from your route as parameters to these method. In that way MVC binds parameters from routes to actual parameters for an actions.

If you want to remove such behavior you just need to iterate over ValueProviderFactories.Factories and remove RouteDataValueProviderFactory from it. But then your routes can have issues with parameters binding.