forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteBuilder.cs
More file actions
180 lines (155 loc) · 7.48 KB
/
Copy pathRouteBuilder.cs
File metadata and controls
180 lines (155 loc) · 7.48 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
// 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.Linq;
using System.Reflection;
using System.Web.Routing;
namespace System.Web.Mvc.Routing
{
/// <summary>
/// Obsolete, use <see cref="System.Web.Mvc.Routing.RouteFactoryAttribute"/> to customize generated attribute
/// routes.
/// </summary>
[Obsolete(
"Obsolete, do not use. To create custom Routes with attribute routing, use " +
"System.Web.Mvc.Routing.RouteFactoryAttribute")]
public class RouteBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="RouteBuilder" /> class using the default inline constraint resolver.
/// </summary>
public RouteBuilder()
: this(new DefaultInlineConstraintResolver())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RouteBuilder" /> class.
/// </summary>
/// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.</param>
public RouteBuilder(IInlineConstraintResolver constraintResolver)
{
if (constraintResolver == null)
{
throw Error.ArgumentNull("constraintResolver");
}
ConstraintResolver = constraintResolver;
}
public IInlineConstraintResolver ConstraintResolver { get; private set; }
/// <summary>
/// Builds an <see cref="Route"/> for a particular controller.
/// </summary>
/// <param name="routeTemplate">The tokenized route template for the route.</param>
/// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param>
/// <returns>The generated <see cref="Route"/>.</returns>
public Route BuildDirectRoute(string routeTemplate, ControllerDescriptor controllerDescriptor)
{
if (routeTemplate == null)
{
throw Error.ArgumentNull("routeTemplate");
}
if (controllerDescriptor == null)
{
throw Error.ArgumentNull("controllerDescriptor");
}
string controllerName = controllerDescriptor.ControllerName;
RouteAreaAttribute area = controllerDescriptor.GetAreaFrom();
string areaName = controllerDescriptor.GetAreaName(area);
RouteValueDictionary defaults = new RouteValueDictionary
{
{ "controller", controllerName }
};
Type controllerType = controllerDescriptor.ControllerType;
RouteValueDictionary dataTokens = new RouteValueDictionary();
if (areaName != null)
{
dataTokens.Add(RouteDataTokenKeys.Area, areaName);
dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
if (controllerType != null)
{
dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { controllerType.Namespace });
}
}
RouteValueDictionary constraints = new RouteValueDictionary();
string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);
Route route = new Route(detokenizedRouteTemplate, new MvcRouteHandler())
{
Defaults = defaults,
Constraints = constraints,
DataTokens = dataTokens
};
return route;
}
/// <summary>
/// Builds an <see cref="Route"/> for a particular action.
/// </summary>
/// <param name="routeTemplate">The tokenized route template for the route.</param>
/// <param name="allowedMethods">The HTTP methods supported by the route. A null value specify that all possible methods are supported.</param>
/// <param name="controllerName">The name of the associated controller.</param>
/// <param name="actionName">The name of the associated action.</param>
/// <param name="targetMethod">The method that the route attribute has been applied on.</param>
/// <param name="areaName"></param>
/// <returns>The generated <see cref="Route"/>.</returns>
public Route BuildDirectRoute(string routeTemplate, IEnumerable<string> allowedMethods, string controllerName, string actionName, MethodInfo targetMethod, string areaName)
{
if (routeTemplate == null)
{
throw Error.ArgumentNull("routeTemplate");
}
if (controllerName == null)
{
throw Error.ArgumentNull("controllerName");
}
if (actionName == null)
{
throw Error.ArgumentNull("actionName");
}
RouteValueDictionary defaults = new RouteValueDictionary
{
{ "controller", controllerName },
{ "action", actionName }
};
RouteValueDictionary constraints = new RouteValueDictionary();
if (allowedMethods != null)
{
string[] array = allowedMethods.ToArray();
if (array.Length > 0)
{
// Current method constraint implementation is inefficient since it matches before running the constraint.
// Consider checking the HTTP method first in a custom route as a performance optimization.
constraints.Add("httpMethod", new HttpMethodConstraint(array));
}
}
RouteValueDictionary dataTokens = new RouteValueDictionary();
if (areaName != null)
{
dataTokens.Add(RouteDataTokenKeys.Area, areaName);
dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
if (targetMethod.DeclaringType != null)
{
dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { targetMethod.DeclaringType.Namespace });
}
}
string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);
return BuildDirectRoute(defaults, constraints, dataTokens, detokenizedRouteTemplate, targetMethod);
}
/// <summary>
/// Builds an <see cref="Route"/>.
/// </summary>
/// <param name="defaults">The route defaults.</param>
/// <param name="constraints">The route constraints.</param>
/// <param name="dataTokens"></param>
/// <param name="routeTemplate">The detokenized route template.</param>
/// <param name="targetMethod">The method that the route attribute has been applied on.</param>
/// <returns>The generated <see cref="Route"/>.</returns>
public virtual Route BuildDirectRoute(RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, string routeTemplate, MethodInfo targetMethod)
{
Route route = new Route(routeTemplate, new MvcRouteHandler())
{
Defaults = defaults,
Constraints = constraints,
DataTokens = dataTokens
};
return route;
}
}
}