forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubRouteCollection.cs
More file actions
105 lines (91 loc) · 3.09 KB
/
Copy pathSubRouteCollection.cs
File metadata and controls
105 lines (91 loc) · 3.09 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
// 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.Globalization;
using System.Linq;
#if ASPNETWEBAPI
using System.Web.Http.Properties;
using TRoute = System.Web.Http.Routing.IHttpRoute;
using TRouteEntry = System.Web.Http.Routing.RouteEntry;
#else
using System.Web.Mvc.Properties;
using TRoute = System.Web.Routing.Route;
using TRouteEntry = System.Web.Mvc.Routing.RouteEntry;
#endif
#if ASPNETWEBAPI
namespace System.Web.Http.Routing
#else
namespace System.Web.Mvc.Routing
#endif
{
/// <summary>Represents a collection of route entries and the routes they contain.</summary>
/// <remarks>
/// This is used in attribute routing, where we want to match multiple routes, and then later
/// disambiguate which one is best.
/// </remarks>
internal class SubRouteCollection : IReadOnlyCollection<TRoute>
{
private readonly List<TRoute> _routes = new List<TRoute>();
private readonly List<TRouteEntry> _entries = new List<TRouteEntry>();
public void Add(TRouteEntry entry)
{
Contract.Assert(entry != null);
TRoute route = entry.Route;
Contract.Assert(route != null);
string name = entry.Name;
if (name != null)
{
TRouteEntry duplicateEntry = _entries.SingleOrDefault((e) => e.Name == name);
if (duplicateEntry != null)
{
ThrowExceptionForDuplicateRouteNames(name, route, duplicateEntry.Route);
}
}
_routes.Add(route);
_entries.Add(entry);
}
public void AddRange(IEnumerable<TRouteEntry> entries)
{
foreach (RouteEntry entry in entries)
{
Add(entry);
}
}
public int Count
{
get { return _entries.Count; }
}
public IEnumerator<TRoute> GetEnumerator()
{
return _routes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_routes).GetEnumerator();
}
public IReadOnlyCollection<TRouteEntry> Entries
{
get { return _entries; }
}
private static void ThrowExceptionForDuplicateRouteNames(string name, TRoute route1, TRoute route2)
{
#if ASPNETWEBAPI
throw new InvalidOperationException(String.Format(
CultureInfo.CurrentCulture,
SRResources.SubRouteCollection_DuplicateRouteName,
name,
route1.RouteTemplate,
route2.RouteTemplate));
#else
throw new InvalidOperationException(String.Format(
CultureInfo.CurrentCulture,
MvcResources.SubRouteCollection_DuplicateRouteName,
name,
route1.Url,
route2.Url));
#endif
}
}
}