1

Before I revert to the earlier package, does anybody know why this can be? I followed this guide pretty much to the letter.

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

Cheers, 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>

RouteConfig

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
            });
    }
Jamie
  • 876
  • 1
  • 10
  • 28

1 Answers1

1

The most likely cause is that you haven't accounted for the country or lang route parameters in your configuration. And since these are ambient values that have nothing to do with page identification, you could use preservedRouteParamters to force them to always match.

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

Currently there is no way to specify preservedRouteParameters globally, so you will need to supply this attribute on every node.

However, be aware that your localized pages won't appear in the XML sitemap endpoint at /sitemap.xml when using preservedRouteParameters.

To fix that, you should fix your route configuration so they will instead appear at {country}/{lang}/sitemap.xml. Add the 2 routes to your configuration to create localized sitemap.xml endpoints, like this:

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

You will need to create an index file so the search engines will know about your localized URLs, and submit the index file to search engines instead of the one at /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>

You should also specify this sitemap index in your robots.txt file so bots of minor search engines can pick it up automatically. For example, if you put the above XML into a file named sitemapindex.xml at the root of your site (where it should be), you can add this line to your robots.txt file (anywhere in the file):

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

Reference: http://www.sitemaps.org/protocol.html#informing

You should also do a 301 redirect for requests of /uk/En/SomeController/SomeAction and /uk/En/SomeController/SomeAction/SomeId to the default versions of these URLs. This can be accomplished either by subclassing RouteBase to make a redirect route or by using an IIS rewrite rule in your web.config file.

Additional Information That Might Be Helpful

There are several things that could cause the SiteMapPath not to appear.

  1. The node intended to match the current page is missing some route or querystring parameters.
  2. Security Trimming is enabled and the user doesn't have permission to view the current node.
  3. The visibility settings are prohibiting the nodes from being rendered.
  4. The HTML helper templates in your /Views/Shared/DisplayTemplates/ folder are prohibiting them from being shown.
  5. The configuration isn't set up to start automatically.

Since you are upgrading, the most likely causes are #1 or #5.

You can rule out #5 by navigating to /sitemap.xml to see if your XML sitemap is rendering.

It might be helpful to temporarily add @Html.MvcSiteMap().SiteMap() to your layout page to see if any nodes are not resolving to the correct URL.

If you do that, #1 is the most likely cause. You must configure every route value to either a specific value...

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

This works best in conjunction with dynamic node providers.

Or you can force a match with any value by using preservedRouteParameters.

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

Have a look at How to Make MvcSiteMapProvider Remember a User's Position for an in depth discussion of this behavior.

Also, keep in mind that if you are using custom attributes that you don't intend to use with routing, you need to add them to the MvcSiteMapProvider_AttributesToIgnore configuration setting.

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

In web.config:

<appSettings>
    <add key="MvcSiteMapProvider_AttributesToIgnore" value="myCustomValue"/>
</appSettings>
Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • sitemap.xml works and shows content. Using the sitemap it looks like it is loading the data correctly. Not sure why it doesn't automatically link to my sitemap URL though to create a breadcrumb. The template in the shared directory states I have no nodes. Is this a breaking change between V3 and V4 I take it? – Jamie Sep 23 '14 at 22:17
  • The node matching has been improved so it will cover more situations, but it also means that it is more strict and therefore does not match any nodes with route values other than action, controller, and area without extra configuration. If you post your node and route configuration, it might be possible to spot the issue. – NightOwl888 Sep 23 '14 at 22:31
  • Added an update to my question, I want a completely static xml file but it can be mapped to a given URL (basically as it was in V3) if that is possible – Jamie Sep 23 '14 at 22:57
  • Sorry for late reply. It was indeed the preservedRouteParameters which fixed the problem. Thanks for this :) – Jamie Sep 27 '14 at 21:01