forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildActionExtensions.cs
More file actions
207 lines (174 loc) · 8.61 KB
/
Copy pathChildActionExtensions.cs
File metadata and controls
207 lines (174 loc) · 8.61 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Mvc.Properties;
using System.Web.Mvc.Routing;
using System.Web.Routing;
using System.Web.WebPages;
namespace System.Web.Mvc.Html
{
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 */, TypeHelper.ObjectToDictionary(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, TypeHelper.ObjectToDictionary(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 */, TypeHelper.ObjectToDictionary(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, TypeHelper.ObjectToDictionary(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.ParentActionViewContextToken] = parentViewContext;
// It's possible that the outgoing route is a direct route - in which case it's not possible to reach using
// the action name and controller name. We need to check for that case to determine if we need to create a
// 'direct route' routedata to reach it.
if (route.IsDirectRoute())
{
// Codeplex-2136 - ControllerContext.IsChildAction returns false inside Controller.Initialize()
//
// We're constructing a 'temp' route data to wrap the route data for the match we're invoking via
// an attribute route. The ControllerContext will look at datatokens to see if it's being invoked
// as a child action.
//
// By sticking the view context on both route data ControllerContext will do the right thing
// at all parts of the pipeline.
var directRouteData = RouteCollectionRoute.CreateDirectRouteMatch(route, new List<RouteData>() { routeData });
directRouteData.DataTokens[ControllerContext.ParentActionViewContextToken] = parentViewContext;
return directRouteData;
}
else
{
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
}
}
}
}