views:

58

answers:

2
+1  A: 

Since you don't have anything to differentiate between view and category items, I'd think about using a default controller which checks if the id is in the categories table and passes control to either the View or the Category controller.

routes.MapRoute(
            "Root",  // Route name
            "/{id}",    // URL with parameters
            new { controller = "default", action = "redirect"}  // Parameter defaults
        );

But if you can live with having "/category/" in your category urls, that will be the more elegant solution on the back end.

somori
+1  A: 

First up, I would suggest coming up with a URL scheme that you are happy with. (seems you have one already)

Then I would use a ControllerFactory that will be responsible of Instantiating and running the right action on the right controller. That is independent of any routes that you define in your route table - in fact it wont matter what you have there since you want your URL to be "database driven". You invoke the controller factory from your Global.asax file :

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new Controllers.ControllerFactory());
}

Then in the GetControllerType method in your ControllerFactory, you inspect the URL with

 RequestContext.RouteData.Values.ContainsKey("keyname") 

to work out the url scheme the user is presenting, and do a database look-up based on that.

If you want to take this one step further, your database can also contain references to the controller to instantiate, but that would be an overkill in your situation. As a quicknote, we use that in a solution where it was important to provide the ability for non-developers to create templates without involving dev - the database held url schemes, controller and views to render on that controller.

While you are at it, if you want to make things more elegant, create a BaseController that your controllers inherit from, and in there set things in your ViewData such as your SEO tags (MetaDescription, Title, etc) - look these up from your database.

Ash M
This might be overkill for 2 tables, but it sounds like a reasonable way forward when you have no idea what and how many tables you will end up with. Whatever way you go though, I'd still be concerned about losing the hackability part of the urls and the uniqueness issues coming from only having a single path.
somori