1

I'm working on a CMS/Webshop engine with a MVC 3 front-end. I want to be able to define url "aliases" for dynamic content/products runtime, and I want to be able to route this URLs to MVC controller actions.
For example I want to be able to define

~/Products/Motherboards/{manufacturer}/{uniqueName}
~/HugeSavings/{uniqueName}
~/Products/{uniqueName} etc.

to map to the same Display(string uniqueName) controller action in ProductsController. These url patterns are dynamic, even the rules for their order or composition is NOT DEFINED at design time, the pattern's rule or pattern's content can change in runtime without restarting the application, they are stored in SQL, but needs to be cached. Each pattern has a target which may be a typical MVC url like

Products/Display/{uniqueName}

or a direct link like

`http://somestuff.com/stuff.aspx?name={uniqueName}.


Every solution I've found used

RegisteredRoutes.Clear(); 
RebuildRoutes();

which is horrible, because of this for adding one new pattern for one product (or product category) I have to query the database for thousands of products and their corresponding patterns.

So, can I change routes without clearing or restarting the app? Can I "inject" some logic to routing WITHOUT having to recode the whole "look up the controller and action and parse the parameters" thing.

TDaver
  • 7,164
  • 5
  • 47
  • 94

2 Answers2

1

Yes, you can add routes later. Just don't RegisteredRoutes.Clear(); them first.

John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • If I want to delete the old or modified ones, how can I identify them? By pattern only? Can I add an ID to routes for example? (because patterns can be modified, not only added and removed, and at time of saving I don't neccessarily know the old pattern, but I know the ID of the pattern being saved) – TDaver Jan 28 '11 at 13:52
  • You can identify routes by Name. http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.aspx – John Farrell Jan 28 '11 at 13:54
1

If you're still intereseted, I just answered a very similar question regarding fully dynamic routing in MVC.

Multilingual URLs with ASP.NET MVC

Community
  • 1
  • 1
Nich Overend
  • 822
  • 8
  • 16
  • THanks, but we've finally went a different route, we put an url rewrite modul BEFORE mvc's routing, so I can have any (and many) url point to a specific action even with setting a parameters, so I no longer have to set the routing inside mvc dinamically. Check out my musings at http://stackoverflow.com/questions/6202682/is-it-possible-to-get-the-controller-and-the-action-not-their-name-based-on-t – TDaver Jun 08 '11 at 18:20