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

Dynamic Routes from database for ASP.NET MVC CMS(来自 ASP.NET MVC CMS 数据库的动态路由)
本文介绍了来自 ASP.NET MVC CMS 数据库的动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

基本上我有一个使用 ASP.NET MVC 构建的 CMS 后端,现在我要转到前端站点,并且需要能够根据输入的路径从我的 cms 数据库中加载页面.

Basically I have a CMS backend I built using ASP.NET MVC and now I'm moving on to the frontend site and need to be able to load pages from my cms database, based on the route entered.

因此,如果用户输入 domain.com/students/information,MVC 将在 pages 表中查看是否存在具有与学生/信息匹配的永久链接的页面,如果存在,它将重定向到页面控制器,然后从数据库中加载页面数据并返回给视图显示.

So if the user enters domain.com/students/information, MVC would look in the pages table to see if a page exists that has a permalink that matches students/information, if so it would redirect to the page controller and then load the page data from the database and return it to the view for display.

到目前为止,我已经尝试了一条包罗万象的路线,但它只适用于两个 URL 段,即/students/information,但不适用于/students/information/fall.我在网上找不到任何关于如何做到这一点的信息,所以我想在这里问一下,在我找到并开源 ASP.NET MVC cms 并剖析代码之前.

So far I have tried to have a catch all route, but it only works for two URL segments, so /students/information, but not /students/information/fall. I can't find anything online on how to accomplish this, so I though I would ask here, before I find and open source ASP.NET MVC cms and dissect the code.

这是我目前的路线配置,但我觉得有更好的方法来做到这一点.

Here is the route configuration I have so far, but I feel there is a better way to do this.

 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 段)并且仍然能够加载具有预定义控制器和操作的核心页面的正确方向.

If anybody can point me in the right direction on how I would go about loading CMS pages from the database, with up to three URL segments, and still be able to load core pages, that have a controller and action predefined.

推荐答案

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

You can use a constraint to decide whether to override the default routing logic.

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;
    }
}

在路由定义中使用它,例如,

use it in route definition like,

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 }
);

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

Now if you have an 'Index' action in 'Page' Controller like,

public ActionResult Index(string permalink)
{
    //load the content from db with permalink
    //show the content with view
}

  1. 所有的 url 都会被第一条路由捕获并被约束验证.
  2. 如果永久链接存在于 db 中,则 url 将由页面控制器中的 Index 操作处理.
  3. 如果不是,约束将失败并且 url 将回退到默认路由(我不知道您在项目中是否有任何其他控制器以及您将如何决定您的 404 逻辑).

编辑

为了避免在Page控制器的Index动作中重新查询cms页面,可以使用HttpContext.Items字典,比如

To avoid re querying the cms page in the Index action in Page controller, one can use the HttpContext.Items dictionary, like

在约束中

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;

然后在行动中,

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

希望这会有所帮助.

这篇关于来自 ASP.NET MVC CMS 数据库的动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)
Using C# 7.1 with MSBuild(将 C# 7.1 与 MSBuild 结合使用)
Build project with Microsoft.Build API(使用 Microsoft.Build API 构建项目)