Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / Html / ChildActionExtensions.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.Html {\r
14     using System.Collections.Generic;\r
15     using System.Globalization;\r
16     using System.IO;\r
17     using System.Linq;\r
18     using System.Web.Mvc.Resources;\r
19     using System.Web.Routing;\r
20 \r
21     public static class ChildActionExtensions {\r
22 \r
23         // Action\r
24 \r
25         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName) {\r
26             return Action(htmlHelper, actionName, null /* controllerName */, null /* routeValues */);\r
27         }\r
28 \r
29         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, object routeValues) {\r
30             return Action(htmlHelper, actionName, null /* controllerName */, new RouteValueDictionary(routeValues));\r
31         }\r
32 \r
33         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues) {\r
34             return Action(htmlHelper, actionName, null /* controllerName */, routeValues);\r
35         }\r
36 \r
37         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName) {\r
38             return Action(htmlHelper, actionName, controllerName, null /* routeValues */);\r
39         }\r
40 \r
41         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) {\r
42             return Action(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues));\r
43         }\r
44 \r
45         public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) {\r
46             StringWriter writer = new StringWriter(CultureInfo.CurrentCulture);\r
47             ActionHelper(htmlHelper, actionName, controllerName, routeValues, writer);\r
48             return MvcHtmlString.Create(writer.ToString());\r
49         }\r
50 \r
51         // RenderAction\r
52 \r
53         public static void RenderAction(this HtmlHelper htmlHelper, string actionName) {\r
54             RenderAction(htmlHelper, actionName, null /* controllerName */, null /* routeValues */);\r
55         }\r
56 \r
57         public static void RenderAction(this HtmlHelper htmlHelper, string actionName, object routeValues) {\r
58             RenderAction(htmlHelper, actionName, null /* controllerName */, new RouteValueDictionary(routeValues));\r
59         }\r
60 \r
61         public static void RenderAction(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues) {\r
62             RenderAction(htmlHelper, actionName, null /* controllerName */, routeValues);\r
63         }\r
64 \r
65         public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName) {\r
66             RenderAction(htmlHelper, actionName, controllerName, null /* routeValues */);\r
67         }\r
68 \r
69         public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) {\r
70             RenderAction(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues));\r
71         }\r
72 \r
73         public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) {\r
74             ActionHelper(htmlHelper, actionName, controllerName, routeValues, htmlHelper.ViewContext.Writer);\r
75         }\r
76 \r
77         // Helpers\r
78 \r
79         internal static void ActionHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, TextWriter textWriter) {\r
80             if (htmlHelper == null) {\r
81                 throw new ArgumentNullException("htmlHelper");\r
82             }\r
83             if (String.IsNullOrEmpty(actionName)) {\r
84                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");\r
85             }\r
86 \r
87             routeValues = MergeDictionaries(routeValues, htmlHelper.ViewContext.RouteData.Values);\r
88             routeValues["action"] = actionName;\r
89             if (!String.IsNullOrEmpty(controllerName)) {\r
90                 routeValues["controller"] = controllerName;\r
91             }\r
92 \r
93             bool usingAreas;\r
94             VirtualPathData vpd = htmlHelper.RouteCollection.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, null /* name */, routeValues, out usingAreas);\r
95             if (vpd == null) {\r
96                 throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);\r
97             }\r
98 \r
99             if (usingAreas) {\r
100                 routeValues.Remove("area");\r
101             }\r
102             RouteData routeData = CreateRouteData(vpd.Route, routeValues, vpd.DataTokens, htmlHelper.ViewContext);\r
103             HttpContextBase httpContext = htmlHelper.ViewContext.HttpContext;\r
104             RequestContext requestContext = new RequestContext(httpContext, routeData);\r
105             ChildActionMvcHandler handler = new ChildActionMvcHandler(requestContext);\r
106             httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), textWriter, true /* preserveForm */);\r
107         }\r
108 \r
109         private static RouteData CreateRouteData(RouteBase route, RouteValueDictionary routeValues, RouteValueDictionary dataTokens, ViewContext parentViewContext) {\r
110             RouteData routeData = new RouteData();\r
111 \r
112             foreach (KeyValuePair<string, object> kvp in routeValues) {\r
113                 routeData.Values.Add(kvp.Key, kvp.Value);\r
114             }\r
115 \r
116             foreach (KeyValuePair<string, object> kvp in dataTokens) {\r
117                 routeData.DataTokens.Add(kvp.Key, kvp.Value);\r
118             }\r
119 \r
120             routeData.Route = route;\r
121             routeData.DataTokens[ControllerContext.PARENT_ACTION_VIEWCONTEXT] = parentViewContext;\r
122             return routeData;\r
123         }\r
124 \r
125         private static RouteValueDictionary MergeDictionaries(params RouteValueDictionary[] dictionaries) {\r
126             // Merge existing route values with the user provided values\r
127             var result = new RouteValueDictionary();\r
128 \r
129             foreach (RouteValueDictionary dictionary in dictionaries.Where(d => d != null)) {\r
130                 foreach (KeyValuePair<string, object> kvp in dictionary) {\r
131                     if (!result.ContainsKey(kvp.Key)) {\r
132                         result.Add(kvp.Key, kvp.Value);\r
133                     }\r
134                 }\r
135             }\r
136 \r
137             return result;\r
138         }\r
139 \r
140         internal class ChildActionMvcHandler : MvcHandler {\r
141             public ChildActionMvcHandler(RequestContext context)\r
142                 : base(context) {\r
143             }\r
144 \r
145             protected internal override void AddVersionHeader(HttpContextBase httpContext) {\r
146                 // No version header for child actions\r
147             }\r
148         }\r
149     }\r
150 }\r