1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Diagnostics.CodeAnalysis;
\r
16 using System.Web.Routing;
\r
18 public static class RouteCollectionExtensions {
\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
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
33 throw new ArgumentNullException("url");
\r
36 IgnoreRouteInternal route = new IgnoreRouteInternal(url) {
\r
37 Constraints = new RouteValueDictionary(constraints)
\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
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
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
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
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
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
80 throw new ArgumentNullException("url");
\r
83 Route route = new Route(url, new MvcRouteHandler()) {
\r
84 Defaults = new RouteValueDictionary(defaults),
\r
85 Constraints = new RouteValueDictionary(constraints)
\r
88 if ((namespaces != null) && (namespaces.Length > 0)) {
\r
89 route.DataTokens = new RouteValueDictionary();
\r
90 route.DataTokens["Namespaces"] = namespaces;
\r
93 routes.Add(name, route);
\r
98 private sealed class IgnoreRouteInternal : Route {
\r
99 public IgnoreRouteInternal(string url)
\r
100 : base(url, new StopRoutingHandler()) {
\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