Url routing with database lookup?

后端 未结 4 655
孤街浪徒
孤街浪徒 2021-01-06 12:25

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

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 13:13

    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").

提交回复
热议问题