-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathRouteCollectionRoute.cs
168 lines (142 loc) · 5.77 KB
/
RouteCollectionRoute.cs
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
// 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;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net.Http;
using System.Web.Http.Properties;
namespace System.Web.Http.Routing
{
/// <summary>
/// A single route that is the composite of multiple "sub routes".
/// </summary>
/// <remarks>
/// Corresponds to the MVC implementation of attribute routing in System.Web.Mvc.Routing.RouteCollectionRoute.
/// </remarks>
internal class RouteCollectionRoute : IHttpRoute, IReadOnlyCollection<IHttpRoute>
{
// Key for accessing SubRoutes on a RouteData.
// We expose this through the RouteData.Values instead of a derived class because
// RouteData can get wrapped in another type, but Values still gets persisted through the wrappers.
// Prefix with a \0 to protect against conflicts with user keys.
public const string SubRouteDataKey = "MS_SubRoutes";
private IReadOnlyCollection<IHttpRoute> _subRoutes;
private static readonly IDictionary<string, object> _empty = EmptyReadOnlyDictionary<string, object>.Value;
public RouteCollectionRoute()
{
}
// This will enumerate all controllers and action descriptors, which will run those
// Initialization hooks, which may try to initialize controller-specific config, which
// may call back to the initialize hook. So guard against that reentrancy.
private bool _beingInitialized;
// deferred hook for initializing the sub routes. The composite route can be added during the middle of
// intializing, but then the actual sub routes can get populated after initialization has finished.
public void EnsureInitialized(Func<IReadOnlyCollection<IHttpRoute>> initializer)
{
if (_beingInitialized && _subRoutes == null)
{
// Avoid reentrant initialization
return;
}
try
{
_beingInitialized = true;
_subRoutes = initializer();
Contract.Assert(_subRoutes != null);
}
finally
{
_beingInitialized = false;
}
}
private IReadOnlyCollection<IHttpRoute> SubRoutes
{
get
{
// Caller should have already explicitly called EnsureInitialize.
// Avoid lazy initilization from within the route table because the route table
// is shared resource and init can happen
if (_subRoutes == null)
{
string msg = Error.Format(SRResources.Object_NotYetInitialized);
throw new InvalidOperationException(msg);
}
return _subRoutes;
}
}
public string RouteTemplate
{
get { return String.Empty; }
}
public IDictionary<string, object> Defaults
{
get { return _empty; }
}
public IDictionary<string, object> Constraints
{
get { return _empty; }
}
public IDictionary<string, object> DataTokens
{
get { return null; }
}
public HttpMessageHandler Handler
{
get
{
return null;
}
}
// Returns null if no match.
// Else, returns a composite route data that encapsulates the possible routes this may match against.
public IHttpRouteData GetRouteData(string virtualPathRoot, HttpRequestMessage request)
{
List<IHttpRouteData> matches = new List<IHttpRouteData>();
foreach (IHttpRoute route in SubRoutes)
{
IHttpRouteData match = route.GetRouteData(virtualPathRoot, request);
if (match != null)
{
matches.Add(match);
}
}
if (matches.Count == 0)
{
return null; // no matches
}
return new RouteCollectionRouteData(this, matches.ToArray());
}
public IHttpVirtualPathData GetVirtualPath(HttpRequestMessage request, IDictionary<string, object> values)
{
// Use LinkGenerationRoute stubs to get placeholders for all the sub routes.
return null;
}
public int Count
{
get { return SubRoutes.Count; }
}
public IEnumerator<IHttpRoute> GetEnumerator()
{
return SubRoutes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return SubRoutes.GetEnumerator();
}
// Represents a union of multiple IHttpRouteDatas.
private class RouteCollectionRouteData : IHttpRouteData
{
public RouteCollectionRouteData(IHttpRoute parent, IHttpRouteData[] subRouteDatas)
{
Route = parent;
// Each sub route may have different values. Callers need to enumerate the subroutes
// and individually query each.
// Find sub-routes via the SubRouteDataKey; don't expose as a property since the RouteData
// can be wrapped in an outer type that doesn't propagate properties.
Values = new HttpRouteValueDictionary() { { SubRouteDataKey, subRouteDatas } };
}
public IHttpRoute Route { get; private set; }
public IDictionary<string, object> Values { get; private set; }
}
}
}