Merge pull request #569 from knocte/fix_cairo_profile_versions_problem
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / RouteCollectionExtensions.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Diagnostics.CodeAnalysis;\r
16     using System.Web.Routing;\r
17 \r
18     public static class RouteCollectionExtensions {\r
19 \r
20         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#",\r
21             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
22         public static void IgnoreRoute(this RouteCollection routes, string url) {\r
23             IgnoreRoute(routes, url, null /* constraints */);\r
24         }\r
25 \r
26         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#",\r
27             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
28         public static void IgnoreRoute(this RouteCollection routes, string url, object constraints) {\r
29             if (routes == null) {\r
30                 throw new ArgumentNullException("routes");\r
31             }\r
32             if (url == null) {\r
33                 throw new ArgumentNullException("url");\r
34             }\r
35 \r
36             IgnoreRouteInternal route = new IgnoreRouteInternal(url) {\r
37                 Constraints = new RouteValueDictionary(constraints)\r
38             };\r
39 \r
40             routes.Add(route);\r
41         }\r
42 \r
43         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
44             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
45         public static Route MapRoute(this RouteCollection routes, string name, string url) {\r
46             return MapRoute(routes, name, url, null /* defaults */, (object)null /* constraints */);\r
47         }\r
48 \r
49         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
50             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
51         public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults) {\r
52             return MapRoute(routes, name, url, defaults, (object)null /* constraints */);\r
53         }\r
54 \r
55         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
56             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
57         public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints) {\r
58             return MapRoute(routes, name, url, defaults, constraints, null /* namespaces */);\r
59         }\r
60 \r
61         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
62             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
63         public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces) {\r
64             return MapRoute(routes, name, url, null /* defaults */, null /* constraints */, namespaces);\r
65         }\r
66 \r
67         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
68             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
69         public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces) {\r
70             return MapRoute(routes, name, url, defaults, null /* constraints */, namespaces);\r
71         }\r
72 \r
73         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#",\r
74             Justification = "This is not a regular URL as it may contain special routing characters.")]\r
75         public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces) {\r
76             if (routes == null) {\r
77                 throw new ArgumentNullException("routes");\r
78             }\r
79             if (url == null) {\r
80                 throw new ArgumentNullException("url");\r
81             }\r
82 \r
83             Route route = new Route(url, new MvcRouteHandler()) {\r
84                 Defaults = new RouteValueDictionary(defaults),\r
85                 Constraints = new RouteValueDictionary(constraints)\r
86             };\r
87 \r
88             if ((namespaces != null) && (namespaces.Length > 0)) {\r
89                 route.DataTokens = new RouteValueDictionary();\r
90                 route.DataTokens["Namespaces"] = namespaces;\r
91             }\r
92 \r
93             routes.Add(name, route);\r
94 \r
95             return route;\r
96         }\r
97 \r
98         private sealed class IgnoreRouteInternal : Route {\r
99             public IgnoreRouteInternal(string url)\r
100                 : base(url, new StopRoutingHandler()) {\r
101             }\r
102 \r
103             public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues) {\r
104                 // Never match during route generation. This avoids the scenario where an IgnoreRoute with\r
105                 // fairly relaxed constraints ends up eagerly matching all generated URLs.\r
106                 return null;\r
107             }\r
108         }\r
109     }\r
110 }\r