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
43 [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
45 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public class RouteCollection : Collection<RouteBase>
49 class Lock : IDisposable
51 //RouteCollection owner;
54 public Lock (RouteCollection owner, bool read)
60 public void Dispose ()
63 // owner.read_lock = null;
65 // owner_write_lock = null;
69 public RouteCollection ()
74 public RouteCollection (VirtualPathProvider virtualPathProvider)
76 // null argument is allowed
77 //provider = virtualPathProvider;
79 read_lock = new Lock (this, true);
80 write_lock = new Lock (this, false);
83 //VirtualPathProvider provider;
84 Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
86 Lock read_lock, write_lock;
88 public RouteBase this [string name] {
97 public bool RouteExistingFiles { get; set; }
99 public void Add (string name, RouteBase item)
101 lock (GetWriteLock ()) {
103 if (!String.IsNullOrEmpty (name))
108 protected override void ClearItems ()
110 lock (GetWriteLock ())
114 public IDisposable GetReadLock ()
119 public RouteData GetRouteData (HttpContextBase httpContext)
121 if (httpContext == null)
122 throw new ArgumentNullException ("httpContext");
124 if (httpContext.Request == null)
125 throw new ArgumentException ("The context does not contain any request data.", "httpContext");
130 if (!RouteExistingFiles) {
131 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
132 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
133 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
140 foreach (RouteBase rb in this) {
141 var rd = rb.GetRouteData (httpContext);
149 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
151 return GetVirtualPath (requestContext, null, values);
154 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
156 if (requestContext == null)
157 throw new ArgumentNullException ("httpContext");
162 VirtualPathData vp = null;
163 if (!String.IsNullOrEmpty (name)) {
164 RouteBase rb = this [name];
166 vp = rb.GetVirtualPath (requestContext, values);
169 throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
172 foreach (RouteBase rb in this) {
173 vp = rb.GetVirtualPath (requestContext, values);
180 string appPath = requestContext.HttpContext.Request.ApplicationPath;
181 if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
184 string pathWithApp = String.Concat (appPath, vp.VirtualPath);
185 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
192 public IDisposable GetWriteLock ()
197 public void Ignore (string url)
202 public void Ignore (string url, object constraints)
205 throw new ArgumentNullException ("url");
207 Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
210 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
212 return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
215 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
217 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
220 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
221 RouteValueDictionary defaults)
223 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
226 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
227 RouteValueDictionary defaults, RouteValueDictionary constraints)
229 return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
232 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
233 RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
235 if (routeUrl == null)
236 throw new ArgumentNullException ("routeUrl");
238 var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
239 Add (routeName, route);
244 protected override void InsertItem (int index, RouteBase item)
246 // FIXME: what happens wrt its name?
247 lock (GetWriteLock ())
248 base.InsertItem (index, item);
251 protected override void RemoveItem (int index)
253 // FIXME: what happens wrt its name?
254 lock (GetWriteLock ()) {
255 string k = GetKey (index);
256 base.RemoveItem (index);
262 protected override void SetItem (int index, RouteBase item)
264 // FIXME: what happens wrt its name?
265 lock (GetWriteLock ()) {
266 string k = GetKey (index);
267 base.SetItem (index, item);
273 string GetKey (int index)
275 var item = this [index];