/
RouteAttribute.cs
156 lines (127 loc) · 5.52 KB
/
RouteAttribute.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
using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using AttributeRouting.Helpers;
namespace AttributeRouting.Web.Mvc
{
/// <summary>
/// The route information for an action in Mvc Controllers.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class RouteAttribute : ActionMethodSelectorAttribute, IRouteAttribute
{
/// <summary>
/// Specify the route information for an action.
/// The route URL will be the name of the action.
/// </summary>
public RouteAttribute()
{
HttpMethods = new string[0];
ActionPrecedence = int.MaxValue;
ControllerPrecedence = int.MaxValue;
SitePrecedence = int.MaxValue;
}
/// <summary>
/// Specify the route information for an action.
/// </summary>
/// <param name="routeUrl">The url that is associated with this action</param>
public RouteAttribute(string routeUrl)
: this()
{
if (routeUrl == null) throw new ArgumentNullException("routeUrl");
RouteUrl = routeUrl;
}
/// <summary>
/// Specify the route information for an action.
/// The route URL will be the name of the action.
/// </summary>
/// <param name="allowedMethods">The httpMethods against which to constrain the route</param>
public RouteAttribute(params HttpVerbs[] allowedMethods)
: this(allowedMethods.Select(m => m.ToString()).ToArray())
{
}
/// <summary>
/// Specify the route information for an action.
/// The route URL will be the name of the action.
/// </summary>
/// <param name="allowedMethods">The httpMethods against which to constrain the route</param>
public RouteAttribute(params string[] allowedMethods)
: this()
{
HttpMethods = allowedMethods.Select(m => m.ToUpperInvariant()).ToArray();
}
/// <summary>
/// Specify the route information for an action.
/// </summary>
/// <param name="routeUrl">The url that is associated with this action</param>
/// <param name="allowedMethods">The httpMethods against which to constrain the route</param>
public RouteAttribute(string routeUrl, params HttpVerbs[] allowedMethods)
: this(routeUrl, allowedMethods.Select(m => m.ToString()).ToArray())
{
}
/// <summary>
/// Specify the route information for an action.
/// </summary>
/// <param name="routeUrl">The url that is associated with this action</param>
/// <param name="allowedMethods">The httpMethods against which to constrain the route</param>
public RouteAttribute(string routeUrl, params string[] allowedMethods)
: this(routeUrl)
{
HttpMethods = allowedMethods.Select(m => m.ToUpperInvariant()).ToArray();
}
public string RouteUrl { get; private set; }
public string[] HttpMethods { get; protected set; }
[Obsolete("Prefer ActionPrecedence for clarity of intent.")]
public int Order
{
get { return ActionPrecedence; }
set { ActionPrecedence = value; }
}
public int ActionPrecedence { get; set; }
[Obsolete("Prefer ControllerPrecedence for clarity of intent.")]
public int Precedence
{
get { return ControllerPrecedence; }
set { ControllerPrecedence = value; }
}
public int ControllerPrecedence { get; set; }
public int SitePrecedence { get; set; }
public string RouteName { get; set; }
public bool IsAbsoluteUrl
{
get { return IgnoreAreaUrl && IgnoreRoutePrefix; }
set { IgnoreAreaUrl = IgnoreRoutePrefix = value; }
}
public string TranslationKey { get; set; }
public bool UseLowercaseRoute
{
get { return UseLowercaseRouteFlag.GetValueOrDefault(); }
set { UseLowercaseRouteFlag = value; }
}
public bool? UseLowercaseRouteFlag { get; private set; }
public bool PreserveCaseForUrlParameters
{
get { return PreserveCaseForUrlParametersFlag.GetValueOrDefault(); }
set { PreserveCaseForUrlParametersFlag = value; }
}
public bool? PreserveCaseForUrlParametersFlag { get; private set; }
public bool AppendTrailingSlash
{
get { return AppendTrailingSlashFlag.GetValueOrDefault(); }
set { AppendTrailingSlashFlag = value; }
}
public bool? AppendTrailingSlashFlag { get; private set; }
public bool IgnoreRoutePrefix { get; set; }
public bool IgnoreAreaUrl { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null) throw new ArgumentNullException("controllerContext");
// If not constrained by a method, then accept always!
if (!HttpMethods.Any())
return true;
var httpMethod = controllerContext.HttpContext.Request.GetHttpMethodOverride();
return HttpMethods.Any(m => m.ValueEquals(httpMethod));
}
}
}