Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / RouteValuesHelpers.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System.Collections.Generic;\r
15     using System.Web.Routing;\r
16 \r
17     internal static class RouteValuesHelpers {\r
18         public static RouteValueDictionary GetRouteValues(RouteValueDictionary routeValues) {\r
19             return (routeValues != null) ? new RouteValueDictionary(routeValues) : new RouteValueDictionary();\r
20         }\r
21 \r
22         public static RouteValueDictionary MergeRouteValues(string actionName, string controllerName, RouteValueDictionary implicitRouteValues, RouteValueDictionary routeValues, bool includeImplicitMvcValues) {\r
23             // Create a new dictionary containing implicit and auto-generated values\r
24             RouteValueDictionary mergedRouteValues = new RouteValueDictionary();\r
25 \r
26             if (includeImplicitMvcValues) {\r
27                 // We only include MVC-specific values like 'controller' and 'action' if we are generating an action link.\r
28                 // If we are generating a route link [as to MapRoute("Foo", "any/url", new { controller = ... })], including\r
29                 // the current controller name will cause the route match to fail if the current controller is not the same\r
30                 // as the destination controller.\r
31 \r
32                 object implicitValue;\r
33                 if (implicitRouteValues != null && implicitRouteValues.TryGetValue("action", out implicitValue)) {\r
34                     mergedRouteValues["action"] = implicitValue;\r
35                 }\r
36 \r
37                 if (implicitRouteValues != null && implicitRouteValues.TryGetValue("controller", out implicitValue)) {\r
38                     mergedRouteValues["controller"] = implicitValue;\r
39                 }\r
40             }\r
41 \r
42             // Merge values from the user's dictionary/object\r
43             if (routeValues != null) {\r
44                 foreach (KeyValuePair<string, object> routeElement in GetRouteValues(routeValues)) {\r
45                     mergedRouteValues[routeElement.Key] = routeElement.Value;\r
46                 }\r
47             }\r
48 \r
49             // Merge explicit parameters when not null\r
50             if (actionName != null) {\r
51                 mergedRouteValues["action"] = actionName;\r
52             }\r
53 \r
54             if (controllerName != null) {\r
55                 mergedRouteValues["controller"] = controllerName;\r
56             }\r
57 \r
58             return mergedRouteValues;\r
59         }\r
60     }\r
61 }\r