【问题标题】:MVC Site Map Provider - one node, two breadcrumbsMVC Site Map Provider - 一个节点,两个面包屑
【发布时间】:2014-04-18 16:42:43
【问题描述】:

我正在使用来自 Github 的 MvcSiteMapProvider 在我的项目中生成面包屑。我有两个地方可以从不同的控制器调用动作,并且面包屑必须显示调用控制器。

基于多个来源(mvc 3 sitemap provider- multiple paths pointing to same nodeMVCSiteMapProvider breadcrumbs incorrect parent node idMVCSiteMapProvider Dynamic Node always returns the first node for all pages in the breadcrumbs 以及这些文章的外部引用),我已从使用文件 Mvc.sitemap 切换到使用 MvcSiteMapNodeAttribute 装饰我的操作。我还将正确的设置放入我的web.config 文件中。

<add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="Solution.Project" />
<add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
<add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
<add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true"/>
<add key="MvcSiteMapProvider_EnableSiteMapFile" value="false"/>

但是,面包屑无法识别这两条路径。

节点的属性是,从根节点开始:

(in controller Home)
[MvcSiteMapNodeAttribute(Title = "Home", Key = "Home")]

(in controller Path1)
[MvcSiteMapNodeAttribute(Title = "Path 1", Key = "Home.Path1", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Title = "Action", Key = "Home.Path1.Action", ParentKey = "Home.Path1", Route="Home.Path1")]
[MvcSiteMapNodeAttribute(Title = "Action", Key = "Home.Path2.Action", ParentKey = "Home.Path2", Route="Home.Path2")]

(in controller Path2)
[MvcSiteMapNodeAttribute(Title = "Path 2", Key = "Home.Path2", ParentKey = "Home")]
    return RedirectToAction("Action", "Path1", new { Route = "Home.Path2" } );

无论我从Path1 还是Path2 调用Action,面包屑总是显示Home &gt; Path 1 &gt; Action

几天来,我一直在与一位同事合作,但我们不知道为什么不同的键和正确的路线没有按预期工作。

【问题讨论】:

    标签: asp.net-mvc-4 breadcrumbs mvcsitemapprovider


    【解决方案1】:

    不可能在 SiteMap 的 2 个不同位置使用单个节点或单个 URL,因为在查找“当前”节点时,第一个匹配总是获胜。

    但是,您可以通过对同一页面使用 2 个不同的 URL(和 2 个不同的节点)来解决此问题。这可以通过向 URL 添加 1 个或多个查询字符串参数(在这种情况下,两个 URL 将调用相同的操作)、使用返回第一个结果的第二个操作(不是重定向)或更多高级方法是修改您的路线,以便您有 2 个完全不同的 URL 指向相同的操作。一旦您解决了该问题并为每个导航路径配置了唯一的 URL,并为每个节点配置了一组唯一的路由值,它应该可以按预期工作。

    另外,请注意,您可能需要使用 Attributes 属性来指定任何自定义路由值(例如“id”),这些值是您尝试匹配的路由的一部分。

    请查看以下页面以获取帮助。请注意,SEO 功能教程包含一个可下载的演示。

    Mvcsitemapprovider Multiple paths to the single page http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features#canonical-tag https://github.com/maartenba/MvcSiteMapProvider/wiki/Multiple-Navigation-Paths-to-a-Single-Page http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position

    如果页面面向 Internet,您还应该使用 CanonicalTag HTML 帮助程序来确保搜索引擎知道您打算将相同的内容放在 2 个不同的 URL 上,并且您可以指定哪个是“主”URL。您只需将 CanonicalTag 助手添加到页面的 HEAD 部分...

    <head>
        <meta charset="utf-8" />
        <title>@Html.MvcSiteMap().SiteMapTitle() - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Html.MvcSiteMap().CanonicalTag()
        @Html.MvcSiteMap().MetaRobotsTag()
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    

    然后在other(s)上为CanonicalKey指定主页的key。

    [MvcSiteMapNodeAttribute(Title = "Action", Key = "Home.Path1.Action", ParentKey = "Home.Path1", Route="Home.Path1")]
    [MvcSiteMapNodeAttribute(Title = "Action", Key = "Home.Path2.Action", ParentKey = "Home.Path2", Route="Home.Path2", CanonicalKey="Home.Path1.Action")]
    

    还有一件事——Route 参数是在生成 URL 时通过名称在你的路由配置中指定一个特定的路由。但是您仍然需要向调用相关操作方法的 MVC 提供具有该名称的路由。由于您没有发布路线,因此无法判断这是否是您在示例中所做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多