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