Question

I'm trying to create the sitemap.xml file automatically for my Asp.Net Mvc 5 website. I have installed the MvcSiteMapProvider.MVC5 4.6.1 package from Nuget and I have read the getting started guide. To my understanding I have to add ALL the controllers and their actions to its configuration file. However, somewhere in the doc, it said that the package automatically keeps track of the visited URLs and generates their nodes in the sitemap.xml, "for free"! I visited a few pages and refreshed the sitemap.xml file but it didn't change. Am I missing something? Does this package really provide dynamic sitemap.xml files?

Was it helpful?

Solution

First of all, the \sitemap.xml endpoint is not a "file", it is a controller action that dynamically generates the XML based on the configuration of the SiteMap. It generates the XML to submit to search engines as per sitemaps.org.

Being that this endpoint is for search engines, it doesn't make any sense to keep track of a user's visits on the site and show the pages the user visited in this result. Instead, it contains all of the pages that are configured (either by URL or by controller, action and other route values).

However, the main purpose of MvcSiteMapProvider is to provide menus and breadcrumb trails to individual pages. Again, this doesn't work by tracking a user's position, it works by determining the relative position (in relation to other nodes) of the URL within the SiteMap. For more information, read How to Make MvcSiteMapProvider Remember a User's Position.

You don't necessarily have to add all of the controller actions to MvcSiteMapProvider, technically you only have to add the URLs that you need indexed by search engines. And then you get the sitemaps XML for free.

However, for pages that you don't need indexed (administration pages, for example) it is possible to use a combination of preservedRouteParameters, [TitleAttribute], and ISiteMapNodeVisibilityProvider to fake the breadcrumb trail so you don't have to add a node for every record in the database to make it function, as described in the above article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top