This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
MvcAreaRouteBuilderExtensions.cs
141 lines (132 loc) · 6.63 KB
/
MvcAreaRouteBuilderExtensions.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
// 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;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extension methods for <see cref="IRouteBuilder"/>.
/// </summary>
public static class MvcAreaRouteBuilderExtensions
{
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/> and <paramref name="template"/>.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IRouteBuilder MapAreaRoute(
this IRouteBuilder routeBuilder,
string name,
string areaName,
string template)
{
MapAreaRoute(routeBuilder, name, areaName, template, defaults: null, constraints: null, dataTokens: null);
return routeBuilder;
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>, and
/// <paramref name="defaults"/>.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IRouteBuilder MapAreaRoute(
this IRouteBuilder routeBuilder,
string name,
string areaName,
string template,
object defaults)
{
MapAreaRoute(routeBuilder, name, areaName, template, defaults, constraints: null, dataTokens: null);
return routeBuilder;
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>,
/// <paramref name="defaults"/>, and <paramref name="constraints"/>.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the route. The object's properties represent the names and
/// values of the constraints.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IRouteBuilder MapAreaRoute(
this IRouteBuilder routeBuilder,
string name,
string areaName,
string template,
object defaults,
object constraints)
{
MapAreaRoute(routeBuilder, name, areaName, template, defaults, constraints, dataTokens: null);
return routeBuilder;
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>,
/// <paramref name="defaults"/>, <paramref name="constraints"/>, and <paramref name="dataTokens"/>.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the route. The object's properties represent the names and
/// values of the constraints.
/// </param>
/// <param name="dataTokens">
/// An object that contains data tokens for the route. The object's properties represent the names and
/// values of the data tokens.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IRouteBuilder MapAreaRoute(
this IRouteBuilder routeBuilder,
string name,
string areaName,
string template,
object defaults,
object constraints,
object dataTokens)
{
if (routeBuilder == null)
{
throw new ArgumentNullException(nameof(routeBuilder));
}
if (string.IsNullOrEmpty(areaName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(areaName));
}
var defaultsDictionary = new RouteValueDictionary(defaults);
defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName;
var constraintsDictionary = new RouteValueDictionary(constraints);
constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName);
routeBuilder.MapRoute(name, template, defaultsDictionary, constraintsDictionary, dataTokens);
return routeBuilder;
}
}
}