ASP.NET MVC CMS数据库的动态路由

Car*_*eis 67 c# routes asp.net-mvc-4

基本上我有一个使用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" }
        );          
    }
Run Code Online (Sandbox Code Playgroud)

如果有人能指出我如何从数据库加载CMS页面,最多有三个URL段,并且仍然能够加载具有预定义控制器和操作的核心页面.

sha*_*kib 123

您可以使用约束来决定是否覆盖默认路由逻辑.

public class CmsUrlConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var db = new MvcCMS.Models.MvcCMSContext();
        if (values[parameterName] != null)
        {
            var permalink = values[parameterName].ToString();
            return db.CMSPages.Any(p => p.Permalink == permalink);
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在路线定义中使用它,比如

routes.MapRoute(
    name: "CmsRoute",
    url: "{*permalink}",
    defaults: new {controller = "Page", action = "Index"},
    constraints: new { permalink = new CmsUrlConstraint() }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

现在,如果您在"页面"控制器中有"索引"操作,例如,

public ActionResult Index(string permalink)
{
    //load the content from db with permalink
    //show the content with view
}
Run Code Online (Sandbox Code Playgroud)
  1. 所有网址都将被第一条路线捕获并通过约束进行验证.
  2. 如果db中存在永久链接,则url将由Page controller中的Index操作处理.
  3. 如果不是约束将失败并且url将回退到默认路由(我不知道你是否在项目中有任何其他控制器以及你将如何决定你的404逻辑).

编辑

为了避免IndexPage控制器中的操作中重新查询cms页面,可以使用HttpContext.Items字典,例如

在约束中

var db = new MvcCMS.Models.MvcCMSContext();
if (values[parameterName] != null)
{
    var permalink = values[parameterName].ToString();
    var page =  db.CMSPages.Where(p => p.Permalink == permalink).FirstOrDefault();
    if(page != null)
    {
        HttpContext.Items["cmspage"] = page;
        return true;
    }
    return false;
}
return false;
Run Code Online (Sandbox Code Playgroud)

然后在行动中,

public ActionResult Index(string permalink)
{
    var page = HttpContext.Items["cmspage"] as CMSPage;
    //show the content with view
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 真棒它工作得很好,只需要添加一个检查if(values [parameterName]!= null),否则完美!谢谢 :) (3认同)