5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
35 using System.Runtime.CompilerServices;
36 using System.Security.Permissions;
38 using System.Web.Hosting;
40 namespace System.Web.Routing
42 [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public class RouteCollection : Collection<RouteBase>
47 class Lock : IDisposable
49 //RouteCollection owner;
52 public Lock (RouteCollection owner, bool read)
58 public void Dispose ()
61 // owner.read_lock = null;
63 // owner_write_lock = null;
67 public RouteCollection ()
72 public RouteCollection (VirtualPathProvider virtualPathProvider)
74 // null argument is allowed
75 //provider = virtualPathProvider;
77 read_lock = new Lock (this, true);
78 write_lock = new Lock (this, false);
81 //VirtualPathProvider provider;
82 Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
84 Lock read_lock, write_lock;
86 public RouteBase this [string name] {
95 public bool RouteExistingFiles { get; set; }
97 public void Add (string name, RouteBase item)
99 lock (GetWriteLock ()) {
101 if (!String.IsNullOrEmpty (name))
106 protected override void ClearItems ()
108 lock (GetWriteLock ())
112 public IDisposable GetReadLock ()
117 public RouteData GetRouteData (HttpContextBase httpContext)
119 if (httpContext == null)
120 throw new ArgumentNullException ("httpContext");
122 if (httpContext.Request == null)
123 throw new ArgumentException ("The context does not contain any request data.", "httpContext");
126 if (!RouteExistingFiles) {
127 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
128 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
129 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
132 foreach (RouteBase rb in this) {
133 var rd = rb.GetRouteData (httpContext);
141 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
143 return GetVirtualPath (requestContext, null, values);
146 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
148 if (requestContext == null)
149 throw new ArgumentNullException ("httpContext");
150 VirtualPathData vp = null;
151 if (!String.IsNullOrEmpty (name)) {
152 RouteBase rb = this [name];
154 vp = rb.GetVirtualPath (requestContext, values);
156 throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
158 foreach (RouteBase rb in this) {
159 vp = rb.GetVirtualPath (requestContext, values);
166 string appPath = requestContext.HttpContext.Request.ApplicationPath;
167 if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
170 string pathWithApp = String.Concat (appPath, vp.VirtualPath);
171 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
178 public IDisposable GetWriteLock ()
182 public void Ignore (string url)
187 public void Ignore (string url, object constraints)
190 throw new ArgumentNullException ("url");
192 Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
195 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
197 return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
200 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
202 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
205 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
206 RouteValueDictionary defaults)
208 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
211 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
212 RouteValueDictionary defaults, RouteValueDictionary constraints)
214 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
217 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
218 RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
220 if (routeUrl == null)
221 throw new ArgumentNullException ("routeUrl");
223 var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
224 Add (routeName, route);
228 protected override void InsertItem (int index, RouteBase item)
230 // FIXME: what happens wrt its name?
231 lock (GetWriteLock ())
232 base.InsertItem (index, item);
235 protected override void RemoveItem (int index)
237 // FIXME: what happens wrt its name?
238 lock (GetWriteLock ()) {
239 string k = GetKey (index);
240 base.RemoveItem (index);
246 protected override void SetItem (int index, RouteBase item)
248 // FIXME: what happens wrt its name?
249 lock (GetWriteLock ()) {
250 string k = GetKey (index);
251 base.SetItem (index, item);
257 string GetKey (int index)
259 var item = this [index];