forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectRouteCandidate.cs
More file actions
235 lines (203 loc) · 8.22 KB
/
Copy pathDirectRouteCandidate.cs
File metadata and controls
235 lines (203 loc) · 8.22 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
// 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.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc.Async;
using System.Web.Mvc.Properties;
using System.Web.Routing;
namespace System.Web.Mvc.Routing
{
/// <summary>
/// Represents a candidate action found via a direct route.
/// </summary>
/// <remarks>
/// Members are get/set for testability of the 'best-match' algorithm.
/// </remarks>
internal class DirectRouteCandidate
{
public ActionDescriptor ActionDescriptor
{
get;
set;
}
public IEnumerable<ActionNameSelector> ActionNameSelectors
{
get;
set;
}
public IEnumerable<ActionSelector> ActionSelectors
{
get;
set;
}
public ControllerDescriptor ControllerDescriptor
{
get;
set;
}
public bool HasActionNameSelectors
{
get
{
return ActionNameSelectors != null && ActionNameSelectors.Any();
}
}
public bool HasActionSelectors
{
get
{
return ActionSelectors != null && ActionSelectors.Any();
}
}
public int Order
{
get;
set;
}
public decimal Precedence
{
get;
set;
}
public RouteData RouteData
{
get;
set;
}
public static DirectRouteCandidate SelectBestCandidate(List<DirectRouteCandidate> candidates, ControllerContext controllerContext)
{
Debug.Assert(controllerContext != null);
Debug.Assert(candidates != null);
// These filters will allow actions to opt-out of execution via the provided public extensibility points.
List<DirectRouteCandidate> filteredByActionName = ApplyActionNameFilters(candidates, controllerContext);
List<DirectRouteCandidate> applicableCandidates = ApplyActionSelectors(filteredByActionName, controllerContext);
// At this point all of the remaining actions are applicable - now we're just trying to find the
// most specific match.
//
// Order is first, because it's the 'override' to our algorithm
List<DirectRouteCandidate> filteredByOrder = FilterByOrder(applicableCandidates);
List<DirectRouteCandidate> filteredByPrecedence = FilterByPrecedence(filteredByOrder);
if (filteredByPrecedence.Count == 0)
{
return null;
}
else if (filteredByPrecedence.Count == 1)
{
return filteredByPrecedence[0];
}
else
{
throw CreateAmbiguiousMatchException(candidates);
}
}
private static AmbiguousMatchException CreateAmbiguiousMatchException(List<DirectRouteCandidate> candidates)
{
string ambiguityList = CreateAmbiguousMatchList(candidates);
string message = String.Format(
CultureInfo.CurrentCulture,
MvcResources.DirectRoute_AmbiguousMatch,
ambiguityList);
return new AmbiguousMatchException(message);
}
protected static string CreateAmbiguousMatchList(IEnumerable<DirectRouteCandidate> candidates)
{
StringBuilder exceptionMessageBuilder = new StringBuilder();
foreach (DirectRouteCandidate candidate in candidates)
{
MethodInfo method = null;
ReflectedActionDescriptor reflectedActionDescriptor = candidate.ActionDescriptor as ReflectedActionDescriptor;
if (reflectedActionDescriptor == null)
{
ReflectedAsyncActionDescriptor reflectedAsyncActionDescriptor = candidate.ActionDescriptor as ReflectedAsyncActionDescriptor;
if (reflectedAsyncActionDescriptor != null)
{
method = reflectedAsyncActionDescriptor.AsyncMethodInfo;
}
}
else
{
method = reflectedActionDescriptor.MethodInfo;
}
string controllerAction = method == null ? candidate.ActionDescriptor.ActionName : Convert.ToString(method, CultureInfo.CurrentCulture);
string controllerType = method.DeclaringType.FullName;
exceptionMessageBuilder.AppendLine();
exceptionMessageBuilder.AppendFormat(CultureInfo.CurrentCulture, MvcResources.ActionMethodSelector_AmbiguousMatchType, controllerAction, controllerType);
}
return exceptionMessageBuilder.ToString();
}
private static List<DirectRouteCandidate> ApplyActionNameFilters(List<DirectRouteCandidate> candidates, ControllerContext controllerContext)
{
List<DirectRouteCandidate> filtered = new List<DirectRouteCandidate>();
foreach (DirectRouteCandidate candidate in candidates)
{
string actionName;
candidate.RouteData.Values.TryGetValue<string>("action", out actionName);
if (candidate.HasActionNameSelectors)
{
// For the sake of consistency - we still want to call the action name selectors even if
// this route was matched without providing an action name.
actionName = actionName ?? candidate.ActionDescriptor.ActionName;
if (candidate.ActionNameSelectors.All(selector => selector(controllerContext, actionName)))
{
filtered.Add(candidate);
}
}
else if (actionName != null)
{
if (String.Equals(actionName, candidate.ActionDescriptor.ActionName, StringComparison.OrdinalIgnoreCase))
{
filtered.Add(candidate);
}
}
else
{
// No name-based filtering applies for this route.
filtered.Add(candidate);
}
}
return filtered;
}
private static List<DirectRouteCandidate> ApplyActionSelectors(List<DirectRouteCandidate> candidates, ControllerContext controllerContext)
{
List<DirectRouteCandidate> matchesWithActionSelectors = new List<DirectRouteCandidate>();
List<DirectRouteCandidate> matchesWithoutActionSelectors = new List<DirectRouteCandidate>();
foreach (DirectRouteCandidate candidate in candidates)
{
if (candidate.HasActionSelectors)
{
if (candidate.ActionSelectors.All(selector => selector(controllerContext)))
{
matchesWithActionSelectors.Add(candidate);
}
}
else
{
matchesWithoutActionSelectors.Add(candidate);
}
}
return matchesWithActionSelectors.Any() ? matchesWithActionSelectors : matchesWithoutActionSelectors;
}
private static List<DirectRouteCandidate> FilterByOrder(List<DirectRouteCandidate> candidates)
{
if (!candidates.Any())
{
return candidates;
}
int minimum = candidates.Min(c => c.Order);
return candidates.Where(c => c.Order == minimum).AsList();
}
private static List<DirectRouteCandidate> FilterByPrecedence(List<DirectRouteCandidate> candidates)
{
if (!candidates.Any())
{
return candidates;
}
decimal minimum = candidates.Min(c => c.Precedence);
return candidates.Where(c => c.Precedence == minimum).AsList();
}
}
}