forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteCollectionRoute.cs
More file actions
97 lines (84 loc) · 3.32 KB
/
Copy pathRouteCollectionRoute.cs
File metadata and controls
97 lines (84 loc) · 3.32 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
// 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;
using System.Linq;
using System.Web.Routing;
namespace System.Web.Mvc.Routing
{
/// <summary>
/// A single route that is the composite of multiple "sub routes".
/// </summary>
/// <remarks>
/// Corresponds to the Web API implementation of attribute routing in System.Web.Http.Routing.RouteCollectionRoute.
/// </remarks>
internal class RouteCollectionRoute : RouteBase, IReadOnlyCollection<RouteBase>
{
private readonly IReadOnlyCollection<RouteBase> _subRoutes;
public RouteCollectionRoute(IReadOnlyCollection<RouteBase> subRoutes)
{
if (subRoutes == null)
{
throw new ArgumentNullException("subRoutes");
}
_subRoutes = subRoutes;
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
List<RouteData> matches = new List<RouteData>();
foreach (RouteBase route in _subRoutes)
{
var match = route.GetRouteData(httpContext);
if (match != null)
{
matches.Add(match);
}
}
return CreateDirectRouteMatch(this, matches);
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
// Link generation is not supported via the RouteCollectionRoute - see LinkGenerationRoute.
return null;
}
public int Count
{
get { return _subRoutes.Count; }
}
public IEnumerator<RouteBase> GetEnumerator()
{
return _subRoutes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _subRoutes.GetEnumerator();
}
public static RouteData CreateDirectRouteMatch(RouteBase route, List<RouteData> matches)
{
if (matches.Count == 0)
{
return null;
}
else
{
var routeData = new RouteData();
routeData.Route = route;
routeData.RouteHandler = new MvcRouteHandler();
routeData.SetDirectRouteMatches(matches);
// At a few points in the code (MvcRouteHandler, MvcHandler) we need to look up the controller
// by name. For the purposes of error handling/debugging, it's helpful if we can have a name
// in this code to pass through.
//
// Inside the DefaultControllerFactory we'll double check the route data and throw if we have
// multiple controller matches, but for now let's just use the controller of the first match.
ControllerDescriptor controllerDescriptor = matches[0].GetTargetControllerDescriptor();
if (controllerDescriptor != null)
{
routeData.Values[RouteDataTokenKeys.Controller] = controllerDescriptor.ControllerName;
}
return routeData;
}
}
}
}