forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionMethodSelectorBase.cs
More file actions
293 lines (251 loc) · 11.6 KB
/
Copy pathActionMethodSelectorBase.cs
File metadata and controls
293 lines (251 loc) · 11.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc.Async;
using System.Web.Mvc.Properties;
using System.Web.Mvc.Routing;
using System.Web.Routing;
namespace System.Web.Mvc
{
// Common base class for Async and Sync action selectors
internal abstract class ActionMethodSelectorBase
{
private StandardRouteActionMethodCache _standardRouteCache;
protected void Initialize(Type controllerType)
{
ControllerType = controllerType;
var allMethods = ControllerType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public);
ActionMethods = Array.FindAll(allMethods, IsValidActionMethod);
// The attribute routing mapper will remove methods from this set as they are mapped.
// The lookup tables are initialized lazily to ensure that direct routing's changes are respected.
StandardRouteMethods = new HashSet<MethodInfo>(ActionMethods);
}
public Type ControllerType { get; private set; }
/// <summary>
/// All action methods.
/// </summary>
public MethodInfo[] ActionMethods { get; private set; }
/// <summary>
/// Methods with no direct route, reachable via standard routing only
/// </summary>
public HashSet<MethodInfo> StandardRouteMethods { get; private set; }
/// <summary>
/// Methods which have ActionNameSelectorAttributes - these methods have dynamic functionality
/// and might choose to opt-in to any request.
/// </summary>
public MethodInfo[] AliasedMethods
{
get
{
return StandardRouteCache.AliasedMethods;
}
}
/// <summary>
/// Methods which do not have ActionNameSelectorAttributes - these are selected statically by name.
/// </summary>
public ILookup<string, MethodInfo> NonAliasedMethods
{
get
{
return StandardRouteCache.NonAliasedMethods;
}
}
private StandardRouteActionMethodCache StandardRouteCache
{
get
{
if (_standardRouteCache == null)
{
// This data structure is immutable, so it's safe for multiple threads to race to create it.
_standardRouteCache = CreateStandardRouteCache();
}
return _standardRouteCache;
}
}
protected AmbiguousMatchException CreateAmbiguousActionMatchException(IEnumerable<MethodInfo> ambiguousMethods, string actionName)
{
string ambiguityList = CreateAmbiguousMatchList(ambiguousMethods);
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ActionMethodSelector_AmbiguousMatch,
actionName, ControllerType.Name, ambiguityList);
return new AmbiguousMatchException(message);
}
protected AmbiguousMatchException CreateAmbiguousMethodMatchException(IEnumerable<MethodInfo> ambiguousMethods, string methodName)
{
string ambiguityList = CreateAmbiguousMatchList(ambiguousMethods);
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.AsyncActionMethodSelector_AmbiguousMethodMatch,
methodName, ControllerType.Name, ambiguityList);
return new AmbiguousMatchException(message);
}
protected static string CreateAmbiguousMatchList(IEnumerable<MethodInfo> ambiguousMethods)
{
StringBuilder exceptionMessageBuilder = new StringBuilder();
foreach (MethodInfo methodInfo in ambiguousMethods)
{
string controllerAction = Convert.ToString(methodInfo, CultureInfo.CurrentCulture);
string controllerType = methodInfo.DeclaringType.FullName;
exceptionMessageBuilder.AppendLine();
exceptionMessageBuilder.AppendFormat(CultureInfo.CurrentCulture, MvcResources.ActionMethodSelector_AmbiguousMatchType, controllerAction, controllerType);
}
return exceptionMessageBuilder.ToString();
}
private static bool IsMethodDecoratedWithAliasingAttribute(MethodInfo methodInfo)
{
return methodInfo.IsDefined(typeof(ActionNameSelectorAttribute), true /* inherit */);
}
protected abstract bool IsValidActionMethod(MethodInfo methodInfo);
// Get the method name (before applying Aliasing attributes).
protected virtual string GetCanonicalMethodName(MethodInfo methodInfo)
{
string methodName = methodInfo.Name;
return methodName;
}
private StandardRouteActionMethodCache CreateStandardRouteCache()
{
var cache = new StandardRouteActionMethodCache();
cache.AliasedMethods = StandardRouteMethods.Where(IsMethodDecoratedWithAliasingAttribute).ToArray();
cache.NonAliasedMethods = StandardRouteMethods
.Except(cache.AliasedMethods)
.ToLookup(GetCanonicalMethodName, StringComparer.OrdinalIgnoreCase);
return cache;
}
protected List<MethodInfo> FindActionMethods(ControllerContext controllerContext, string actionName)
{
List<MethodInfo> matches = new List<MethodInfo>();
var cache = StandardRouteCache;
// Performance sensitive, so avoid foreach
for (int i = 0; i < cache.AliasedMethods.Length; i++)
{
MethodInfo method = cache.AliasedMethods[i];
if (IsMatchingAliasedMethod(method, controllerContext, actionName))
{
matches.Add(method);
}
}
matches.AddRange(cache.NonAliasedMethods[actionName]);
RunSelectionFilters(controllerContext, matches);
return matches;
}
protected static bool IsMatchingAliasedMethod(MethodInfo method, ControllerContext controllerContext, string actionName)
{
// return if aliased method is opting in to this request
// to opt in, all attributes defined on the method must return true
ReadOnlyCollection<ActionNameSelectorAttribute> attributes = ReflectedAttributeCache.GetActionNameSelectorAttributes(method);
// Caching count is faster for ReadOnlyCollection
int attributeCount = attributes.Count;
// Performance sensitive, so avoid foreach
for (int i = 0; i < attributeCount; i++)
{
if (!attributes[i].IsValidName(controllerContext, actionName, method))
{
return false;
}
}
return true;
}
protected static bool IsValidMethodSelector(ReadOnlyCollection<ActionMethodSelectorAttribute> attributes, ControllerContext controllerContext, MethodInfo method)
{
int attributeCount = attributes.Count;
Contract.Assert(attributeCount > 0);
for (int i = 0; i < attributeCount; i++)
{
if (!attributes[i].IsValidForRequest(controllerContext, method))
{
return false;
}
}
return true;
}
protected static void RunSelectionFilters(ControllerContext controllerContext, List<MethodInfo> methodInfos)
{
// Filter depending on the selection attribute.
// Methods with valid selection attributes override all others.
// Methods with one or more invalid selection attributes are removed.
bool hasValidSelectionAttributes = false;
// loop backwards for fastest removal
for (int i = methodInfos.Count - 1; i >= 0; i--)
{
MethodInfo methodInfo = methodInfos[i];
ReadOnlyCollection<ActionMethodSelectorAttribute> attrs = ReflectedAttributeCache.GetActionMethodSelectorAttributesCollection(methodInfo);
if (attrs.Count == 0)
{
// case 1: this method does not have a MethodSelectionAttribute
if (hasValidSelectionAttributes)
{
// if there is already method with a valid selection attribute, remove method without one
methodInfos.RemoveAt(i);
}
}
else if (IsValidMethodSelector(attrs, controllerContext, methodInfo))
{
// case 2: this method has MethodSelectionAttributes that are all valid
// if a matching action method had a selection attribute, consider it more specific than a matching action method
// without a selection attribute
if (!hasValidSelectionAttributes)
{
// when the first selection attribute is discovered, remove any items later in the list without selection attributes
if (i + 1 < methodInfos.Count)
{
methodInfos.RemoveFrom(i + 1);
}
hasValidSelectionAttributes = true;
}
}
else
{
// case 3: this method has a method selection attribute but it is not valid
// remove the method since it is opting out of this request
methodInfos.RemoveAt(i);
}
}
}
// Get the action name for the method.
public string GetActionName(MethodInfo methodInfo)
{
// Check for ActionName attribute
object[] nameAttributes = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), inherit: true);
if (nameAttributes.Length > 0)
{
ActionNameAttribute nameAttribute = nameAttributes[0] as ActionNameAttribute;
if (nameAttribute != null)
{
return nameAttribute.Name;
}
}
return GetCanonicalMethodName(methodInfo);
}
public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName)
{
if (controllerContext == null)
{
throw Error.ArgumentNull("controllerContext");
}
if (actionName == null)
{
throw Error.ArgumentNull("actionName");
}
List<MethodInfo> finalMethods = FindActionMethods(controllerContext, actionName);
switch (finalMethods.Count)
{
case 0:
return null;
case 1:
return finalMethods[0];
default:
throw CreateAmbiguousActionMatchException(finalMethods, actionName);
}
}
private class StandardRouteActionMethodCache
{
public MethodInfo[] AliasedMethods { get; set; }
public ILookup<string, MethodInfo> NonAliasedMethods { get; set; }
}
}
}