【问题标题】:Upgrading to V4 has caused @Html.MvcSiteMap().SiteMapPath() to not return anything升级到 V4 导致 @Html.MvcSiteMap().SiteMapPath() 不返回任何内容
【发布时间】:2014-11-17 11:27:29
【问题描述】:

在我恢复到以前的包之前,有人知道为什么会这样吗?我几乎完全按照本指南进行操作。

https://github.com/maartenba/MvcSiteMapProvider/wiki/Upgrading-from-v3-to-v4

干杯, J

mvc.sitemap

<?xml version="1.0" encoding="utf-8" ?>

<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="Opportunity Stream" controller="TaskStream" action="Index"/>
    <mvcSiteMapNode title="Opportunity Stream" controller="TaskStream" action="IndexNew"/>
    <mvcSiteMapNode title="Appointment" controller="Appointment" action="Index"/>
    <mvcSiteMapNode title="Vehicle Search" controller="VehicleSearch" action="Index"/>
    <mvcSiteMapNode title="Stock" controller="Stock" action="Index"/>
    <mvcSiteMapNode title="Admin" controller="Admin" action="Index">
        <mvcSiteMapNode title="Team Management" controller="Admin" action="TeamManagement">
            <mvcSiteMapNode title="Manage Team Member" controller="Admin" action="TeamManagementDetails"/>
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Site Management" controller="Site" action="Index">
            <mvcSiteMapNode title="Site" controller="Site" action="SiteOptions" preservedRouteParameters="id">
                <mvcSiteMapNode title="Default Calendar" controller="Site" action="DefaultCalendar"/>
                <mvcSiteMapNode title="Exception Calendar" controller="Site" action="ExceptionCalendar"/>
                <mvcSiteMapNode title="Manage Site" controller="Site" action="Details"/>
                <mvcSiteMapNode title="Manage Site" controller="Site" action="Edit"/>
            </mvcSiteMapNode>
            <mvcSiteMapNode title="Create Site" controller="Site" action="Create"/>
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Approve Leave Requests" controller="LeaveRequest" action="Index"/>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="Auction" controller="Auction" action="Index"/>
    <mvcSiteMapNode title="Employee" controller="Employee" action="Index">
        <mvcSiteMapNode title="Calendar Exceptions" controller="Site" action="TeamExceptions"/>
        <mvcSiteMapNode title="Employee Detail" controller="Employee" action="Detail" clickable="false"/>
        <mvcSiteMapNode title="Employee Detail" controller="Employee" action="Edit" clickable="false"/>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="User Profile" controller="UserProfile" action="Index">
        <mvcSiteMapNode title="My Holidays" controller="UserProfile" action="MyHolidays"/>
        <mvcSiteMapNode title="Create Leave Request" controller="LeaveRequest" action="Create"/>
    </mvcSiteMapNode>

</mvcSiteMapNode>

路由配置

public static void RegisterRoutes(RouteCollection routes)
    {
        XmlSiteMapController.RegisterRoutes(RouteTable.Routes);

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*alljs}", new
        {
            alljs = @".*\.js(/.*)?"
        });
        routes.IgnoreRoute("{*allpng}", new
        {
            allpng = @".*\.png(/.*)?"
        });
        routes.IgnoreRoute("{*allcss}", new
        {
            allcss = @".*\.css(/.*)?"
        });
        routes.IgnoreRoute("{*allgif}", new
        {
            allgif = @".*\.gif(/.*)?"
        });
        routes.IgnoreRoute("{*alljpg}", new
        {
            alljpg = @".*\.jpg(/.*)?"
        });

        routes.MapRoute("Default", "{controller}/{action}/{id}",
            new
            {
                country = "uk",
                lang = "En",
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });
        routes.MapRoute("localizedDefault", "{country}/{lang}/{controller}/{action}/{id}",
            new
            {
                country = "uk",
                lang = "En",
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });
    }

【问题讨论】:

    标签: mvcsitemapprovider


    【解决方案1】:

    最可能的原因是您没有在配置中考虑countrylang 路由参数。而且由于这些是与页面标识无关的环境值,您可以使用preservedRouteParamters 来强制它们始终匹配。

    <mvcSiteMapNode title="Home" controller="Home" action="Index" preservedRouteParameters="country,lang">
    

    目前无法全局指定preservedRouteParameters,因此您需要在每个节点上提供此属性。

    但是,请注意,使用preservedRouteParameters 时,您的本地化页面不会出现在/sitemap.xml 的XML 站点地图端点中。

    要解决这个问题,您应该修复您的路由配置,这样它们就会出现在{country}/{lang}/sitemap.xml。将 2 条路由添加到您的配置中以创建本地化的 sitemap.xml 端点,如下所示:

    public static void RegisterRoutes(RouteCollection routes)
    {
        XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
    
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*alljs}", new
        {
            alljs = @".*\.js(/.*)?"
        });
        routes.IgnoreRoute("{*allpng}", new
        {
            allpng = @".*\.png(/.*)?"
        });
            routes.IgnoreRoute("{*allcss}", new
        {
            allcss = @".*\.css(/.*)?"
        });
            routes.IgnoreRoute("{*allgif}", new
        {
        allgif = @".*\.gif(/.*)?"
        });
            routes.IgnoreRoute("{*alljpg}", new
        {
            alljpg = @".*\.jpg(/.*)?"
        });
    
        // Localized XML Sitemap routes for MvcSiteMapProvider
        routes.MapRoute("localizedSitemap", "{country}/{lang}/sitemap.xml",
            new
            {
                country = "uk",
                lang = "En",
                controller = "XmlSiteMap",
                action = "Index",
                page = 0
            });
        routes.MapRoute("localizedSitemapPage", "{country}/{lang}/sitemap-{page}.xml",
            new
            {
                country = "uk",
                lang = "En",
                controller = "XmlSiteMap",
                action = "Index",
                page = 0
            });
    
        routes.MapRoute("Default", "{controller}/{action}/{id}",
            new
            {
                country = "uk",
                lang = "En",
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });
        routes.MapRoute("localizedDefault", "{country}/{lang}/{controller}/{action}/{id}",
            new
            {
                country = "uk",
                lang = "En",
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });
    }
    

    您需要创建一个索引文件,以便搜索引擎知道您的本地化 URL,并将索引文件提交给搜索引擎,而不是 /sitemap.xml 的那个。

    <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <sitemap>
            <!-- specifies the default culture sitemap En-uk -->
            <loc>http://www.example.com/sitemap.xml</loc>
        </sitemap>
        <sitemap>
            <loc>http://www.example.com/de/De/sitemap.xml</loc>
        </sitemap>
        <sitemap>
            <loc>http://www.example.com/mx/Es/sitemap.xml</loc>
        </sitemap>
    </sitemapindex>
    

    您还应该在 robots.txt 文件中指定此站点地图索引,以便小型搜索引擎的机器人可以自动获取它。例如,如果您将上述 XML 放入站点根目录(应位于的位置)名为 sitemapindex.xml 的文件中,则可以将此行添加到您的 robots.txt 文件中(文件中的任何位置):

    Sitemap: http://www.example.com/sitemapindex.xml
    

    参考:http://www.sitemaps.org/protocol.html#informing

    应该还对 /uk/En/SomeController/SomeAction/uk/En/SomeController/SomeAction/SomeId 的请求执行 301 重定向到这些 URL 的默认版本。这可以通过subclassing RouteBase 来实现重定向路由或通过在您的web.config 文件中使用IIS rewrite rule 来实现。

    可能有帮助的其他信息

    有几件事可能会导致 SiteMapPath 不显示。

    1. 旨在匹配当前页面的节点缺少某些路由或查询字符串参数。
    2. 安全修整已启用,用户无权查看当前节点。
    3. 可见性设置禁止渲染节点。
    4. /Views/Shared/DisplayTemplates/ 文件夹中的 HTML 帮助程序模板禁止显示它们。
    5. 配置未设置为自动启动。

    由于您正在升级,最可能的原因是 #1 或 #5。

    您可以通过导航到/sitemap.xml 来查看您的 XML 站点地图是否正在呈现,从而排除 #5。

    暂时将@Html.MvcSiteMap().SiteMap() 添加到您的布局页面以查看是否有任何节点未解析到正确的 URL 可能会有所帮助。

    如果您这样做,#1 是最可能的原因。您必须将每个路由值配置为特定值...

    <mvcSiteMapNode title="Foo" action="Index" controller="Customer" id="3"/>
    

    这与dynamic node providers 结合使用效果最佳。

    或者您可以使用preservedRouteParameters 强制匹配任何值。

    <mvcSiteMapNode title="Foo" action="Index" controller="Customer" preservedRouteParameters="id" />
    

    查看How to Make MvcSiteMapProvider Remember a User's Position 以深入讨论此行为。

    另外,请记住,如果您使用不打算用于路由的自定义属性,则需要将它们添加到 MvcSiteMapProvider_AttributesToIgnore 配置设置中。

    <mvcSiteMapNode title="Foo" action="Index" controller="Customer" myCustomValue="Something" />
    

    在 web.config 中:

    <appSettings>
        <add key="MvcSiteMapProvider_AttributesToIgnore" value="myCustomValue"/>
    </appSettings>
    

    【讨论】:

    • sitemap.xml 工作并显示内容。使用站点地图看起来它正在正确加载数据。不知道为什么它不会自动链接到我的站点地图 URL 以创建面包屑。共享目录中的模板表明我没有节点。这是 V3 和 V4 之间的重大变化吗?
    • 改进了节点匹配,可以覆盖更多的情况,但这也意味着它更加严格,因此不匹配除了action、controller、area以外的任何具有路由值的节点,没有额外的配置。如果您发布节点和路由配置,可能会发现问题。
    • 对我的问题添加了更新,我想要一个完全静态的 xml 文件,但如果可能的话,它可以映射到给定的 URL(基本上就像在 V3 中一样)
    • 抱歉回复晚了。确实是preservedRouteParameters 解决了这个问题。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2017-03-04
    • 2022-11-30
    • 2012-10-04
    • 1970-01-01
    • 2020-04-27
    • 2019-04-06
    相关资源
    最近更新 更多