开发者

Dynamic/Editable MVC routes stored in the database for blog engine

I'm planning to write an MVC blog engine, supporting multiple blogs. When creating a blog the user will be able to choose the path where his blog will be accessed.

For example:

/blogs/firstBlog/

/newprojects/secondBlog/

/foo/bar/thirdblog/

This route info开发者_开发问答rmation will be stored in the database. Instead of MVC using only the routes statically declared in Global.asax.cs, I'd like it to read the routes from the database first. Then if it doesn't find anything, fallback on the routes declared in Global.asax.cs. Is this possible? If yes what would you suggest to accomplish that?

Thanks


I'd declare your default route as normal, and then read the database and call the same methods, but rather than passing in hard coded strings, pass in the values from the database:

foreach(RouteDetails routeDetails in routesFromDatabase)
{
    routes.MapRoute(
        routeDetails.Name,                                             
        routeDetails.Route,
        routeDetails.Defaults);
}

You'll need to map the tables properly and populate the fields in the imagined object 'RouteDetails', including the Defaults dictionary (which would probably be the most complicated).

You can find a couple of examples here:

  • asp.net mvc: store routes in the database
  • Editable MVC Routes Apache Style
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜