forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultDirectRouteProvider.cs
More file actions
329 lines (288 loc) · 14 KB
/
Copy pathDefaultDirectRouteProvider.cs
File metadata and controls
329 lines (288 loc) · 14 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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.Contracts;
using System.Linq;
using System.Reflection;
using System.Web.Mvc.Properties;
using System.Web.Routing;
namespace System.Web.Mvc.Routing
{
public class DefaultDirectRouteProvider : IDirectRouteProvider
{
/// <summary>
/// Gets direct routes for the given controller descriptor and action descriptors based on
/// <see cref="IDirectRouteFactory"/> attributes.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <param name="actionDescriptors">The action descriptors for all actions.</param>
/// <param name="constraintResolver">The constraint resolver.</param>
/// <returns>A set of route entries.</returns>
/// <remarks>
/// The implementation returns route entries for the given controller and actions.
///
/// Any actions that have associated <see cref="IDirectRouteFactory"/> instances will produce route
/// entries that route direct to those actions.
///
/// Any actions that do not have an associated <see cref="IDirectRouteFactory"/> instances will be
/// associated with the controller. If the controller has any associated <see cref="IDirectRouteProvider"/>
/// instances, then route entries will be created for the controller and associated actions.
/// </remarks>
public virtual IReadOnlyList<RouteEntry> GetDirectRoutes(
ControllerDescriptor controllerDescriptor,
IReadOnlyList<ActionDescriptor> actionDescriptors,
IInlineConstraintResolver constraintResolver)
{
List<RouteEntry> entries = new List<RouteEntry>();
List<ActionDescriptor> actionsWithoutRoutes = new List<ActionDescriptor>();
foreach (ActionDescriptor action in actionDescriptors)
{
IReadOnlyList<IDirectRouteFactory> factories = GetActionRouteFactories(action);
if (factories != null && factories.Count > 0)
{
IReadOnlyCollection<RouteEntry> actionEntries = GetActionDirectRoutes(action, factories, constraintResolver);
if (actionEntries != null)
{
entries.AddRange(actionEntries);
}
}
else
{
// IF there are no routes on the specific action, attach it to the controller routes (if any).
actionsWithoutRoutes.Add(action);
}
}
if (actionsWithoutRoutes.Count > 0)
{
IReadOnlyList<IDirectRouteFactory> controllerFactories = GetControllerRouteFactories(controllerDescriptor);
if (controllerFactories != null && controllerFactories.Count > 0)
{
IReadOnlyCollection<RouteEntry> controllerEntries = GetControllerDirectRoutes(
controllerDescriptor,
actionsWithoutRoutes,
controllerFactories,
constraintResolver);
if (controllerEntries != null)
{
entries.AddRange(controllerEntries);
}
}
}
return entries;
}
/// <summary>
/// Gets route factories for the given controller descriptor.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <returns>A set of route factories.</returns>
/// <remarks>
/// The implementation returns <see cref="IDirectRouteFactory"/> instances based on attributes on the controller.
/// </remarks>
protected virtual IReadOnlyList<IDirectRouteFactory> GetControllerRouteFactories(ControllerDescriptor controllerDescriptor)
{
object[] attributes = controllerDescriptor.GetCustomAttributes(inherit: false);
IEnumerable<IDirectRouteFactory> newFactories = attributes.OfType<IDirectRouteFactory>();
IEnumerable<IRouteInfoProvider> oldProviders = attributes.OfType<IRouteInfoProvider>();
List<IDirectRouteFactory> combined = new List<IDirectRouteFactory>();
combined.AddRange(newFactories);
foreach (IRouteInfoProvider oldProvider in oldProviders)
{
if (oldProvider is IDirectRouteFactory)
{
continue;
}
combined.Add(new RouteInfoDirectRouteFactory(oldProvider));
}
return combined;
}
/// <summary>
/// Gets a set of route factories for the given action descriptor.
/// </summary>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <returns>A set of route factories.</returns>
/// <remarks>
/// The implementation returns <see cref="IDirectRouteFactory"/> instances based on attributes on the action. Returns
/// null if the action was defined on a base class of this controller.
/// </remarks>
protected virtual IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(ActionDescriptor actionDescriptor)
{
// Skip Route attributes on inherited actions.
IMethodInfoActionDescriptor methodInfoActionDescriptor = actionDescriptor as IMethodInfoActionDescriptor;
if (methodInfoActionDescriptor != null &&
methodInfoActionDescriptor.MethodInfo != null &&
actionDescriptor.ControllerDescriptor != null &&
methodInfoActionDescriptor.MethodInfo.DeclaringType != actionDescriptor.ControllerDescriptor.ControllerType)
{
return null;
}
object[] attributes = actionDescriptor.GetCustomAttributes(inherit: false);
IEnumerable<IDirectRouteFactory> newFactories = attributes.OfType<IDirectRouteFactory>();
IEnumerable<IRouteInfoProvider> oldProviders = attributes.OfType<IRouteInfoProvider>();
List<IDirectRouteFactory> combined = new List<IDirectRouteFactory>();
combined.AddRange(newFactories);
foreach (IRouteInfoProvider oldProvider in oldProviders)
{
if (oldProvider is IDirectRouteFactory)
{
continue;
}
combined.Add(new RouteInfoDirectRouteFactory(oldProvider));
}
return combined;
}
/// <summary>
/// Creates <see cref="RouteEntry"/> instances based on the provided factories, controller and actions. The route
/// entries provided direct routing to the provided controller and can reach the set of provided actions.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <param name="actionDescriptors">The action descriptors.</param>
/// <param name="factories">The direct route factories.</param>
/// <param name="constraintResolver">The constraint resolver.</param>
/// <returns>A set of route entries.</returns>
protected virtual IReadOnlyList<RouteEntry> GetControllerDirectRoutes(
ControllerDescriptor controllerDescriptor,
IReadOnlyList<ActionDescriptor> actionDescriptors,
IReadOnlyList<IDirectRouteFactory> factories,
IInlineConstraintResolver constraintResolver)
{
return CreateRouteEntries(
GetAreaPrefix(controllerDescriptor),
GetRoutePrefix(controllerDescriptor),
factories,
actionDescriptors,
constraintResolver,
targetIsAction: false);
}
/// <summary>
/// Creates <see cref="RouteEntry"/> instances based on the provided factories and action. The route entries
/// provide direct routing to the provided action.
/// </summary>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <param name="factories">The direct route factories.</param>
/// <param name="constraintResolver">The constraint resolver.</param>
/// <returns>A set of route entries.</returns>
protected virtual IReadOnlyList<RouteEntry> GetActionDirectRoutes(
ActionDescriptor actionDescriptor,
IReadOnlyList<IDirectRouteFactory> factories,
IInlineConstraintResolver constraintResolver)
{
return CreateRouteEntries(
GetAreaPrefix(actionDescriptor.ControllerDescriptor),
GetRoutePrefix(actionDescriptor.ControllerDescriptor),
factories,
new ActionDescriptor[] { actionDescriptor },
constraintResolver,
targetIsAction: true);
}
/// <summary>
/// Gets the route prefix from the provided controller.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <returns>The route prefix or null.</returns>
protected virtual string GetRoutePrefix(ControllerDescriptor controllerDescriptor)
{
IRoutePrefix[] attributes = controllerDescriptor.GetCustomAttributes(inherit: false).OfType<IRoutePrefix>().ToArray();
if (attributes == null)
{
return null;
}
if (attributes.Length > 1)
{
string errorMessage = Error.Format(
MvcResources.RoutePrefix_CannotSupportMultiRoutePrefix,
controllerDescriptor.ControllerType.FullName);
throw new InvalidOperationException(errorMessage);
}
if (attributes.Length == 1)
{
IRoutePrefix attribute = attributes[0];
if (attribute != null)
{
string prefix = attribute.Prefix;
if (prefix == null)
{
string errorMessage = Error.Format(
MvcResources.RoutePrefix_PrefixCannotBeNull,
controllerDescriptor.ControllerType.FullName);
throw new InvalidOperationException(errorMessage);
}
if (prefix.StartsWith("/", StringComparison.Ordinal)
|| prefix.EndsWith("/", StringComparison.Ordinal))
{
string errorMessage = Error.Format(
MvcResources.RoutePrefix_CannotStartOrEnd_WithForwardSlash, prefix,
controllerDescriptor.ControllerName);
throw new InvalidOperationException(errorMessage);
}
return prefix;
}
}
return null;
}
/// <summary>
/// Gets the area prefix from the provided controller.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <returns>The area prefix or null.</returns>
protected virtual string GetAreaPrefix(ControllerDescriptor controllerDescriptor)
{
RouteAreaAttribute area = controllerDescriptor.GetAreaFrom();
string areaName = controllerDescriptor.GetAreaName(area);
string areaPrefix = area != null ? area.AreaPrefix ?? area.AreaName : null;
ValidateAreaPrefixTemplate(areaPrefix, areaName, controllerDescriptor);
return areaPrefix;
}
private static IReadOnlyList<RouteEntry> CreateRouteEntries(
string areaPrefix,
string controllerPrefix,
IReadOnlyCollection<IDirectRouteFactory> factories,
IReadOnlyCollection<ActionDescriptor> actions,
IInlineConstraintResolver constraintResolver,
bool targetIsAction)
{
List<RouteEntry> entries = new List<RouteEntry>();
foreach (IDirectRouteFactory factory in factories)
{
RouteEntry entry = CreateRouteEntry(areaPrefix, controllerPrefix, factory, actions, constraintResolver, targetIsAction);
entries.Add(entry);
}
return entries;
}
// Internal for testing
internal static RouteEntry CreateRouteEntry(
string areaPrefix,
string controllerPrefix,
IDirectRouteFactory factory,
IReadOnlyCollection<ActionDescriptor> actions,
IInlineConstraintResolver constraintResolver,
bool targetIsAction)
{
Contract.Assert(factory != null);
DirectRouteFactoryContext context = new DirectRouteFactoryContext(
areaPrefix,
controllerPrefix,
actions,
constraintResolver,
targetIsAction);
RouteEntry entry = factory.CreateRoute(context);
if (entry == null)
{
throw Error.InvalidOperation(
MvcResources.TypeMethodMustNotReturnNull,
typeof(IDirectRouteFactory).Name,
"CreateRoute");
}
DirectRouteBuilder.ValidateRouteEntry(entry);
return entry;
}
private static void ValidateAreaPrefixTemplate(string areaPrefix, string areaName, ControllerDescriptor controllerDescriptor)
{
if (areaPrefix != null && areaPrefix.EndsWith("/", StringComparison.Ordinal))
{
string errorMessage = Error.Format(MvcResources.RouteAreaPrefix_CannotEnd_WithForwardSlash,
areaPrefix, areaName, controllerDescriptor.ControllerName);
throw new InvalidOperationException(errorMessage);
}
}
}
}