title: Extending the RouteCollection in ASP.NET Routing
body: <p>I have been developing a pure MVC CMS for fun and have run into an annoying bug/feature of ASP.NET routing. </p>
  
  <p>Each <em>dynamic</em> managed page in my CMS is associated with a particular route that is pulled from the database. These are loaded at app start. When a user adds a new page or edits the Url of an existing page I need to be able to edit the RouteTable to insert/edit route accordingly.</p>
  
  <p>The problem is that the new route does not need to be simply added to the end of the RouteCollection, instead it may need to be inserted into a particular position. Seems logical enough except that the RouteCollection contains only a standard <code>Insert(int idx, RouteBase route)</code> method inherited from <code>Collection&lt;T&gt;</code> which doesn't contain the route name. The name of the route is important as I use it throughout to generate action links.</p>
  
  <p>Looking at reflector I can't see an easy way to extend this collection as the _namedMap dictionary is marked as private. I tried chopping the collection at the point of insert and re-adding each item again, however because there is no method to reverse lookup a route's name from the RouteCollection I cannot re-add them with the name they may have had before. So frustrating!!!</p>
  
  <p>Why is the name of the route not a property of the route object?
  Why if MS is serious about us extending MVC and Routing do they make crucial classes hard to extend?</p>
  
  <p>Any suggestions as to the best solution here?</p>
  
  <p><strong>Edit :</strong></p>
  
  <p>Ok maybe I should have been <strong>much much</strong> clearer here. I am not looking for a critique of my CMS design. I appreciate the comments but this is not what I am asking. </p>
  
  <p>Simplified question. How can I insert a named route into the route collection at runtime? The current insert method on the class is insufficient as it does not include the name.</p>
  
  <p>Cheers,</p>
  
  <p>Ian</p>