diff options
Diffstat (limited to 'src/System.Web.Mvc/RouteValuesHelpers.cs')
-rw-r--r-- | src/System.Web.Mvc/RouteValuesHelpers.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/System.Web.Mvc/RouteValuesHelpers.cs b/src/System.Web.Mvc/RouteValuesHelpers.cs new file mode 100644 index 00000000..4e4b6904 --- /dev/null +++ b/src/System.Web.Mvc/RouteValuesHelpers.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Web.Routing; + +namespace System.Web.Mvc +{ + internal static class RouteValuesHelpers + { + public static RouteValueDictionary GetRouteValues(RouteValueDictionary routeValues) + { + return (routeValues != null) ? new RouteValueDictionary(routeValues) : new RouteValueDictionary(); + } + + public static RouteValueDictionary MergeRouteValues(string actionName, string controllerName, RouteValueDictionary implicitRouteValues, RouteValueDictionary routeValues, bool includeImplicitMvcValues) + { + // Create a new dictionary containing implicit and auto-generated values + RouteValueDictionary mergedRouteValues = new RouteValueDictionary(); + + if (includeImplicitMvcValues) + { + // We only include MVC-specific values like 'controller' and 'action' if we are generating an action link. + // If we are generating a route link [as to MapRoute("Foo", "any/url", new { controller = ... })], including + // the current controller name will cause the route match to fail if the current controller is not the same + // as the destination controller. + + object implicitValue; + if (implicitRouteValues != null && implicitRouteValues.TryGetValue("action", out implicitValue)) + { + mergedRouteValues["action"] = implicitValue; + } + + if (implicitRouteValues != null && implicitRouteValues.TryGetValue("controller", out implicitValue)) + { + mergedRouteValues["controller"] = implicitValue; + } + } + + // Merge values from the user's dictionary/object + if (routeValues != null) + { + foreach (KeyValuePair<string, object> routeElement in GetRouteValues(routeValues)) + { + mergedRouteValues[routeElement.Key] = routeElement.Value; + } + } + + // Merge explicit parameters when not null + if (actionName != null) + { + mergedRouteValues["action"] = actionName; + } + + if (controllerName != null) + { + mergedRouteValues["controller"] = controllerName; + } + + return mergedRouteValues; + } + } +} |