forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostedHttpRouteCollection.cs
More file actions
279 lines (238 loc) · 9.18 KB
/
HostedHttpRouteCollection.cs
File metadata and controls
279 lines (238 loc) · 9.18 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
// 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.Net.Http;
using System.Web.Hosting;
using System.Web.Http.Routing;
using System.Web.Http.WebHost.Properties;
using System.Web.Routing;
namespace System.Web.Http.WebHost.Routing
{
internal class HostedHttpRouteCollection : HttpRouteCollection
{
private readonly RouteCollection _routeCollection;
private readonly string _virtualPathRoot;
public HostedHttpRouteCollection(RouteCollection routeCollection)
: this(routeCollection, virtualPathRoot: null)
{
}
public HostedHttpRouteCollection(RouteCollection routeCollection, string virtualPathRoot)
{
if (routeCollection == null)
{
throw Error.ArgumentNull("routeCollection");
}
_routeCollection = routeCollection;
_virtualPathRoot = virtualPathRoot;
}
/// <inheritdoc/>
public override string VirtualPathRoot
{
get
{
if (_virtualPathRoot == null)
{
return HostingEnvironment.ApplicationVirtualPath;
}
else
{
return _virtualPathRoot;
}
}
}
/// <inheritdoc/>
public override int Count
{
get { return _routeCollection.Count; }
}
/// <inheritdoc/>
public override IHttpRoute this[string name]
{
get
{
HttpWebRoute route = _routeCollection[name] as HttpWebRoute;
if (route != null)
{
return route.HttpRoute;
}
throw Error.KeyNotFound();
}
}
/// <inheritdoc/>
public override IHttpRoute this[int index]
{
get
{
HttpWebRoute route = _routeCollection[index] as HttpWebRoute;
if (route != null)
{
return route.HttpRoute;
}
throw Error.ArgumentOutOfRange("index", index, SRResources.RouteCollectionOutOfRange);
}
}
/// <inheritdoc/>
public override IHttpRouteData GetRouteData(HttpRequestMessage request)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
HttpContextBase httpContextBase = request.GetHttpContext();
if (httpContextBase == null)
{
httpContextBase = new HttpRequestMessageContextWrapper(VirtualPathRoot, request);
}
if (httpContextBase.GetHttpRequestMessage() == null)
{
httpContextBase.SetHttpRequestMessage(request);
}
RouteData routeData = _routeCollection.GetRouteData(httpContextBase);
// If the match is from an IgnoreRoute, do not return a RouteData but return a null, which will be treated as a 404 NoRouteMatched.
if (routeData != null && !(routeData.RouteHandler is System.Web.Routing.StopRoutingHandler))
{
return new HostedHttpRouteData(routeData);
}
return null;
}
/// <inheritdoc/>
public override IHttpVirtualPathData GetVirtualPath(HttpRequestMessage request, string name, IDictionary<string, object> values)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
HttpContextBase httpContextBase = request.GetHttpContext();
if (httpContextBase == null)
{
httpContextBase = new HttpRequestMessageContextWrapper(VirtualPathRoot, request);
}
if (httpContextBase.GetHttpRequestMessage() == null)
{
httpContextBase.SetHttpRequestMessage(request);
}
IHttpRouteData routeData = request.GetRouteData();
if (routeData == null)
{
return null;
}
RequestContext requestContext = new RequestContext(httpContextBase, routeData.ToRouteData());
RouteValueDictionary routeValues = values != null ? new RouteValueDictionary(values) : new RouteValueDictionary();
VirtualPathData virtualPathData = _routeCollection.GetVirtualPath(requestContext, name, routeValues);
if (virtualPathData != null)
{
// If the route is not an HttpWebRoute, try getting a virtual path without the httproute key in the route value dictionary
// This ensures that httproute isn't picked up by non-WebAPI routes that might pollute the virtual path with httproute
if (!(virtualPathData.Route is HttpWebRoute))
{
if (routeValues.Remove(HttpWebRoute.HttpRouteKey))
{
VirtualPathData virtualPathDataWithoutHttpRouteValue = _routeCollection.GetVirtualPath(requestContext, name, routeValues);
if (virtualPathDataWithoutHttpRouteValue != null)
{
virtualPathData = virtualPathDataWithoutHttpRouteValue;
}
}
}
return new HostedHttpVirtualPathData(virtualPathData, routeData.Route);
}
return null;
}
/// <inheritdoc/>
public override IHttpRoute CreateRoute(string uriTemplate, IDictionary<string, object> defaults, IDictionary<string, object> constraints, IDictionary<string, object> dataTokens, HttpMessageHandler handler)
{
if (constraints != null)
{
foreach (var constraint in constraints)
{
ValidateConstraint(uriTemplate, constraint.Key, constraint.Value);
}
}
return new HostedHttpRoute(uriTemplate, defaults, constraints, dataTokens, handler);
}
/// <inheritdoc/>
protected override void ValidateConstraint(string routeTemplate, string name, object constraint)
{
// In WebHost the constraint might be IHttpRouteConstraint or IRouteConstraint (System.Web) or a string
HttpWebRoute.ValidateConstraint(routeTemplate, name, constraint);
}
/// <inheritdoc/>
public override void Add(string name, IHttpRoute route)
{
_routeCollection.Add(name, route.ToRoute());
}
/// <inheritdoc/>
public override void Clear()
{
_routeCollection.Clear();
}
/// <inheritdoc/>
public override bool Contains(IHttpRoute item)
{
foreach (RouteBase route in _routeCollection)
{
HttpWebRoute webRoute = route as HttpWebRoute;
if (webRoute != null && webRoute.HttpRoute == item)
{
return true;
}
}
return false;
}
/// <inheritdoc/>
public override bool ContainsKey(string name)
{
return _routeCollection[name] != null;
}
/// <inheritdoc/>
public override void CopyTo(IHttpRoute[] array, int arrayIndex)
{
throw NotSupportedByHostedRouteCollection();
}
/// <inheritdoc/>
public override void CopyTo(KeyValuePair<string, IHttpRoute>[] array, int arrayIndex)
{
throw NotSupportedByRouteCollection();
}
/// <inheritdoc/>
public override void Insert(int index, string name, IHttpRoute value)
{
throw NotSupportedByRouteCollection();
}
/// <inheritdoc/>
public override bool Remove(string name)
{
throw NotSupportedByRouteCollection();
}
/// <inheritdoc/>
public override IEnumerator<IHttpRoute> GetEnumerator()
{
// Here we only care about Web API routes.
return _routeCollection
.OfType<HttpWebRoute>()
.Select(httpWebRoute => httpWebRoute.HttpRoute)
.GetEnumerator();
}
/// <inheritdoc/>
public override bool TryGetValue(string name, out IHttpRoute route)
{
HttpWebRoute rt = _routeCollection[name] as HttpWebRoute;
if (rt != null)
{
route = rt.HttpRoute;
return true;
}
route = null;
return false;
}
private static NotSupportedException NotSupportedByRouteCollection()
{
return Error.NotSupported(SRResources.RouteCollectionNotSupported, typeof(HostedHttpRouteCollection).Name);
}
private static NotSupportedException NotSupportedByHostedRouteCollection()
{
return Error.NotSupported(SRResources.RouteCollectionUseDirectly, typeof(RouteCollection).Name);
}
}
}