The RouteCollection class inherits from Collection<routebase> and wraps a dictionary<string, ROUTEBASE>, so it provides the functionality of both.
By looking at the code, we can see that the data in collection and dictionary are not exactly the same.
1. Route with name exists both in D and in C, and can be retrieved by name via Index property (see Add method)
2. Route with no name exists only in C
3. Delete route, if it is also in D, remove it from D (see RemoveItem method)
4. When setting the route, if D also exists, it is also removed from D (see SetItem method, which requires special attention)
This class shows a very good lock mechanism! See the yellow highlighted section in the code!
The other methods in this class are later said in the Route class (the next).
Using System;
Using System.Collections.Generic;
Using System.Collections.ObjectModel;
Using System.Globalization;
Using System.Security.Permissions;
Using System.Threading;
Using System.Web;
Using System.Web.Hosting; namespace System.Web.Routing {[AspNetHostingPermission (SecurityAction.LinkDemand, level = Aspnethostingpermissionlevel.minimal), AspNetHostingPermission (SecurityAction.InheritanceDemand, level =
Aspnethostingpermissionlevel.minimal)] public class Routecollection:collection<routebase> {//Fields
Private dictionary<string, routebase> _namedmap;
Private ReaderWriterLock _rwlock;
Private VirtualPathProvider _VPP; Methods public RouteCollection (): This (hostingenvironment.virtualpathprovider) {} public Routecollect Ion (VirtualPathProvider virtualpathprovider) {this._namedmap = new dictionary<string, routebase> (StringCo Mparer.
OrdinalIgnoreCase);
This._rwlock = new ReaderWriterLock (); THIS._VPP = VirtualPathProvider; public void Add (string name, routebase item) {if (item = null) throw new Argumentnullexceptio
N ("item"); if (!string. IsNullOrEmpty (name) && This._namedmap.containskey (name)) throw new ArgumentException (string. Format (CultureInfo.CurrentUICulture, Routingresources.routecollection_duplicatename, new object[] {name})
, "name"); Base.
ADD (item); if (!string.
IsNullOrEmpty (name)) This._namedmap[name] = Item;
} protected override void ClearItems () {this._namedmap.clear (); Base.
ClearItems ();
Private RequestContext Getrequestcontext (RequestContext requestcontext) {if (RequestContext!= null)
return requestcontext;
HttpContext current = HttpContext.Current;
if (current = = null) throw new InvalidOperationException (Routingresources.routecollection_requirescontext); return new ReqUestcontext (new Httpcontextwrapper (current), New Routedata ()); Public Routedata Getroutedata (HttpContextBase HttpContext) {if (HttpContext = null) throw new
ArgumentNullException ("HttpContext"); if (httpcontext.request = = null) throw new ArgumentException (Routingresources.routetable_contextmissingrequest, "H
Ttpcontext "); if (!this. Routeexistingfiles) {String apprelativecurrentexecutionfilepath = HttpContext.Request.AppRelativeCurrentExe
Cutionfilepath; if ((Apprelativecurrentexecutionfilepath!= "~/") && (THIS._VPP!= null)) && (THIS._VPP.
FileExists (Apprelativecurrentexecutionfilepath) | | THIS._VPP.
DirectoryExists (Apprelativecurrentexecutionfilepath)) return null; The using (this. Getreadlock ()) foreach (RouteBase base2 in this) {Routedata routedata = base2.
Getroutedata (HttpContext);
if (routedata!= null)return routedata;
return null; private static string Geturlwithapplicationpath (RequestContext requestcontext, string url) {string str = RequestContext.HttpContext.Request.ApplicationPath?? String.
Empty; if (!str.
EndsWith ("/", stringcomparison.ordinalignorecase)) str = str + "/";
return requestContext.HttpContext.Response.ApplyAppPathModifier (str + URL); Public Virtualpathdata GetVirtualPath (RequestContext requestcontext, routevaluedictionary values) {req Uestcontext = this.
Getrequestcontext (RequestContext); using (this. Getreadlock ()) foreach (RouteBase base2 in this) {Virtualpathdata virtualpath = base2.
GetVirtualPath (RequestContext, values); if (virtualpath!= null) {Virtualpath.virtualpath = Geturlwithapplicationpath (RequestContext, Virtu
Alpath.virtualpath);
return virtualpath;
} return null;
}
Public Virtualpathdata GetVirtualPath (RequestContext requestcontext, string name, routevaluedictionary values)
{RouteBase base2;
BOOL Flag; RequestContext = this.
Getrequestcontext (RequestContext); if (string. IsNullOrEmpty (name)) return this.
GetVirtualPath (RequestContext, values); using (this.
Getreadlock ()) flag = This._namedmap.trygetvalue (name, out BASE2); if (!flag) throw new ArgumentException (string. Format (CultureInfo.CurrentUICulture, Routingresources.routecollection_namenotfound, new object[] {name}),
"Name"); Virtualpathdata virtualpath = Base2.
GetVirtualPath (RequestContext, values);
if (virtualpath = null) return null;
Virtualpath.virtualpath = Geturlwithapplicationpath (RequestContext, Virtualpath.virtualpath);
return virtualpath; } protected override void InsertItem (int index, routebase item) {if (item = null) throwNew ArgumentNullException ("item"); if (base. Contains (item)) throw new ArgumentException (string. Format (CultureInfo.CurrentUICulture, Routingresources.routecollection_duplicateentry, New object[0]), "it
Em "); Base.
InsertItem (index, item); } protected override void RemoveItem (int index) {this.
Removeroutename (index); Base.
RemoveItem (index);
private void Removeroutename (int index) {routebase base2 = Base[index]; foreach (keyvaluepair<string, routebase> pair in This._namedmap) if (pair. Value = = base2) {this._namedmap.remove (pair).
Key);
Break } protected override void SetItem (int index, routebase item) {if (item = null) throw new
ArgumentNullException ("item"); if (base. Contains (item)) throw new ArgumentException (string. Format (CultureInfo.CurrentUICulture, Routingresources.routecolleCtion_duplicateentry, New object[0]), "item"); This.
Removeroutename (index); Base.
SetItem (index, item);
}//Properties public routebase this[string name] {get {routebase base2; if (!string.
IsNullOrEmpty (name) && This._namedmap.trygetvalue (name, out Base2)) return base2;
return null;
public bool Routeexistingfiles {get; set;}
Public IDisposable Getreadlock () {This._rwlock.acquirereaderlock (-1);
return new readlockdisposable (This._rwlock);
Public IDisposable Getwritelock () {This._rwlock.acquirewriterlock (-1);
return new writelockdisposable (This._rwlock); }//Nested Types Private class Readlockdisposable:idisposable {//Fields private Readerwrit
Erlock _rwlock;
Methods public readlockdisposable (ReaderWriterLock rwLock) {this._rwlock = RwLock; } void IDisposable.Dispose () {this._rwlock.releasereaderlock (); } private class Writelockdisposable:idisposable {//Fields private ReaderWriterLock _rwloc
K
Methods public writelockdisposable (ReaderWriterLock rwLock) {this._rwlock = RwLock;
} void IDisposable.Dispose () {this._rwlock.releasewriterlock (); } } } }