开发者

mvc.net dynamic urls

I am looking to produce an MVC site which has complete control of the url structure using routing.

        routes.MapRoute(
            "BlogView",                                                        // Route name
            "view/{blogurl}",                                                    // URL with parameters
            new { controller = "view", action = "view", productLink = ""}  // Parameter defaults
        );

        routes.MapRoute(
            "ProductGrid",                                                        // Route name
            "category/{category}",                                                    // URL with parameters
            new { controller = "category", action = "Index", category = "" }  // Pa开发者_如何学编程rameter defaults
        );

I currently have the follwoing urls;

www.myblog.com/view/first-post

www.myblog.com/view/another-post

www.myblog.com/category/code

www.myblog.com/category/example

The first two urls relate to the detail view, the latter two relating ot a category view.

I have a database with the following structure; I ensure that the url (chrUrl) is a unique key. url ( idurl (int), chrURL, chrAction, chrController )

My plan is that it is possible to look up rewrite the route lookup table so that the follwoing urls redirect to the correct view and page in the site;

www.myblog.com/first-post

www.myblog.com/another-post

www.myblog.com/code

www.myblog.com/example

Is this possible? Perofmance aside, is there a problem with this and how shoudl I go about this?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜