views:

266

answers:

4

I want to build a ASP.NET MVC site so that the controller for a specific url is stored in the database instead of the URL.

The reason for that is that i'm building a CMS system and the users should be able to change the template (controller) without changing the URL. I also think that the name of the controller is not relevant for the end users and i want clean URL:s.

I realise that i could just add all routes at application start, but for a system with like 100 000 pages it feels like a bad idea.

Is it possible to store the url:s in the database and make a lookup for each request and then map that request to a specific controller?

A: 

I suppose ASP.NET can do many of the same things as PHP. If so there is a simple approach.

With rewrite rules you can easily send any traffic to any URL of the 100K to the same place. On that destination you could simply use the server variables containing the URL requested by the client and extract the location. Look it up in the DB and send the corresponding data for that URL back to the client on-the-fly.

mr-euro
you'd still need asp.net mvc url routing for this to work
jfar
A: 

"for a system with like 100,000 pages it feels like a bad idea."

It is a bad idea if you are creating a routing system that cannot be reused. The basic {controller}/{action}/{id} schema points you in the direction of reuse. This schema can be extended/revamped/recreated according to your needs.

Instead of thinking about how many pages you have think about how your resources can be grouped.

Instead of creating a heavy routing system why not create an anchor link control (ascx) which allows user to only add valid internal links. Keep a table in the db of your templates and their controllers to populate the control with it.

kjgilla
+2  A: 

Basically you'll have to implement your own IRouteHandler.

Part of the answer and some example code is in Option 3 of this question's answer: http://stackoverflow.com/questions/379558/mvcnet-routing

More information: http://weblogs.asp.net/fredriknormen/archive/2007/11/18/asp-net-mvc-framework-create-your-own-iroutehandler.aspx

jfar
A: 

Why couldn't you just do something like this:

-- Global.asax.cs --

 routes.MapRoute(null,              // Route name
                 "content/{id}",    // URL with parameters               
                 new { Controller = "Content", Action = "Show", Id = (string) null });  // Parameter defaults

-- /Controllers/ContentController.cs --

public class ContentController : Controller
{
    public ActionResult Show(string id)
    {
        // Lookup the 'content' (article, page, blog post, etc) in the repository (database, xml file, etc)
        ContentRepository repository = new ContentRepository();
        Content content = repository.FindContent(id);
        return View(content);
    }
}

Such that a request to your site www.yoursite.com/content/welcome-to-my-first-blog-post would call ContentController.Show("welcome-to-my-first-blog-post").

GuyIncognito
Technically that's a sound solution but wouldn't he/she end up sending a lot of requests through this one method. Seems like an anti-pattern.
kjgilla