1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Html {
\r
14 using System.Collections.Generic;
\r
15 using System.Globalization;
\r
18 using System.Web.Mvc.Resources;
\r
19 using System.Web.Routing;
\r
21 public static class ChildActionExtensions {
\r
25 public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName) {
\r
26 return Action(htmlHelper, actionName, null /* controllerName */, null /* routeValues */);
\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
33 public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues) {
\r
34 return Action(htmlHelper, actionName, null /* controllerName */, routeValues);
\r
37 public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName) {
\r
38 return Action(htmlHelper, actionName, controllerName, null /* routeValues */);
\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
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
53 public static void RenderAction(this HtmlHelper htmlHelper, string actionName) {
\r
54 RenderAction(htmlHelper, actionName, null /* controllerName */, null /* routeValues */);
\r
57 public static void RenderAction(this HtmlHelper htmlHelper, string actionName, object routeValues) {
\r
58 RenderAction(htmlHelper, actionName, null /* controllerName */, new RouteValueDictionary(routeValues));
\r
61 public static void RenderAction(this HtmlHelper htmlHelper, string actionName, RouteValueDictionary routeValues) {
\r
62 RenderAction(htmlHelper, actionName, null /* controllerName */, routeValues);
\r
65 public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName) {
\r
66 RenderAction(htmlHelper, actionName, controllerName, null /* routeValues */);
\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
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
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
83 if (String.IsNullOrEmpty(actionName)) {
\r
84 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
\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
94 VirtualPathData vpd = htmlHelper.RouteCollection.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, null /* name */, routeValues, out usingAreas);
\r
96 throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
\r
100 routeValues.Remove("area");
\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
109 private static RouteData CreateRouteData(RouteBase route, RouteValueDictionary routeValues, RouteValueDictionary dataTokens, ViewContext parentViewContext) {
\r
110 RouteData routeData = new RouteData();
\r
112 foreach (KeyValuePair<string, object> kvp in routeValues) {
\r
113 routeData.Values.Add(kvp.Key, kvp.Value);
\r
116 foreach (KeyValuePair<string, object> kvp in dataTokens) {
\r
117 routeData.DataTokens.Add(kvp.Key, kvp.Value);
\r
120 routeData.Route = route;
\r
121 routeData.DataTokens[ControllerContext.PARENT_ACTION_VIEWCONTEXT] = parentViewContext;
\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
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
140 internal class ChildActionMvcHandler : MvcHandler {
\r
141 public ChildActionMvcHandler(RequestContext context)
\r
145 protected internal override void AddVersionHeader(HttpContextBase httpContext) {
\r
146 // No version header for child actions
\r