基本上我有一个使用ASP.NET MVC构建的CMS后端,现在我正在进入前端站点,需要能够根据输入的路由从我的cms数据库加载页面。
因此,如果用户输入domain.com/students/information,MVC将查看页面表以查看是否存在具有与学生/信息匹配的永久链接的页面,如果是,则将重定向到页面控制器然后加载页面来自数据库的数据并将其返回到视图以供显示。
到目前为止,我已尝试捕获所有路径,但它仅适用于两个URL段,因此/学生/信息,但不是/ students / information / fall。 我在网上找不到任何关于如何实现这一点的内容,所以我会在这里问一下,在我找到并开源ASP.NET MVC cms并剖析代码之前。
这是我到目前为止的路由配置,但我觉得有更好的方法来做到这一点。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default route to handle core pages
routes.MapRoute(null,"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Index" }
);
// CMS route to handle routing to the PageController to check the database for the route.
var db = new MvcCMS.Models.MvcCMSContext();
//var page = db.CMSPages.Where(p => p.Permalink == )
routes.MapRoute(
null,
"{*.}",
new { controller = "Page", action = "Index" }
);
}
如果有人能指出我如何从数据库加载CMS页面,最多有三个URL段,并且仍然能够加载具有预定义控制器和操作的核心页面。