-
Notifications
You must be signed in to change notification settings - Fork 1
Sitemaps XML Protocol Endpoint for Search Engines
Since MvcSiteMapProvider keeps track of your application's URLs, it is the logical place to use as a data source for exporting the URLs for search engine indexing. By default, this feature is enabled and you get it for free. Simply navigate to /sitemap.xml to see its result.
If you don't need this feature, it can be disabled by setting the MvcSiteMapProvider_EnableSitemapsXml setting to false. If using external DI, it can be disabled by removing the following line from MvcSiteMapProviderConfig.cs
//// Register the Sitemaps routes for search engines
//XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
Whenever a sitemap exceeds 35,000 nodes the XmlSiteMapController will automatically split your sitemap into a sitemap index file (/sitemap.xml) which references sub-sitemaps (/sitemap-1.xml, /sitemap-2.xml, etc.) as described on http://www.sitemaps.org/protocol.php.
Note that we chose the number 35,000 as an average cap because we don't actually measure the physical size of the sitemap result, which cannot exceed 10MB.
Whenever a client sends an HTTP request header with Accept-encoding set to a value of gzip or deflate, the XmlSiteMapResult class (which is also used internally in the XmlSiteMapController) will automatically compress the sitemaps XML using GZip compression.
Note: The technique described here can only be done when using an external DI container.
If you need to customize the result (for example, if you need to combine the results of multiple SiteMap objects into one Sitemaps XML result), you can do this easily by building your own custom controller and using the overloads of the Create method of XmlSiteMapResultFactory to change the output.
Let's say you have followed the instructions on Multiple Sitemaps in One Application page to add 2 SiteMaps to your application, and your SiteMap cache keys are now "mainNavigation" and "footerNavigation". You decide you want to combine these two sitemap files into one result so you can submit them to search engines all at once.
The default behavior of XmlSiteMapController is to only include the URLs that match the SiteMapCacheKey for the current HTTP request (as assigned by ISiteMapCacheKeyGenerator). But we can override the default behavior in our custom controller. This is how we would do that.
[AllowAnonymous]
public class CustomXmlSiteMapController
: Controller
{
public CustomXmlSiteMapController(
IXmlSiteMapResultFactory xmlSiteMapResultFactory
)
{
if (xmlSiteMapResultFactory == null)
throw new ArgumentNullException("xmlSiteMapResultFactory");
this.xmlSiteMapResultFactory = xmlSiteMapResultFactory;
}
private readonly IXmlSiteMapResultFactory xmlSiteMapResultFactory;
public ActionResult Index(int page = 0)
{
return xmlSiteMapResultFactory.Create(page, new string[] { "mainNavigation", "footerNavigation" });
}
}
As you can see, we are using an overload of XmlSiteMapFactoryResult that accepts multiple SiteMap cache keys, and we are passing in the 2 specific ones that we want. The SiteMap cache keys can be thought of like the name of a SiteMap instance.
XmlSiteMapResult will automatically filter out any URLs that are exactly the same in the "mainNavigation" and "footerNavigation" SiteMaps.
Now, we need to disable the standard routes that MvcSiteMapProvider uses as described near the top of this page. Note that if you are using a DI modules-only package, this line may be somewhere else in your application's composition root.
// MvcSiteMapProviderConfig.cs file
//// Register the Sitemaps routes for search engines
//XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
Then we need to register our own routes to point to our new custom controller, one for the sitemap index, and one for the paged sitemaps. Note you can change the URL to whatever scheme you prefer.
routes.MapRoute(
name: "SiteMap",
url: "sitemap.xml",
defaults: new { controller = "CustomXmlSiteMap", action = "Index", page = 0 }
);
routes.MapRoute(
name: "SiteMap-Paged",
url: "sitemap-{page}.xml",
defaults: new { controller = "CustomXmlSiteMap", action = "Index", page = 0 }
);
We generally don't need to touch the DI configuration because IXmlSiteMapResultFactory can be resolved by the default MvcSiteMapProvider registration logic. However, there may be a need to register it explicitly in some DI configurations.
Now if you run the application, you will see the results of both SiteMap instances in a single combined XML result that conforms to the Sitemaps protocol.
- [Wiki Home] (.)
- [Release Notes] (https://github.com/maartenba/MvcSiteMapProvider/releases)
Want to contribute? See our [Contributing to MvcSiteMapProvider] (https://github.com/maartenba/MvcSiteMapProvider/blob/master/CONTRIBUTING.md) guide.
- [Upgrading from v3 to v4] (Upgrading-from-v3-to-v4)
- [Routing Basics] (Routing-Basics)
- Configuring MvcSiteMapProvider
- Defining Sitemap Nodes in XML
- Defining Sitemap Nodes using .NET Attributes
- Defining Sitemap Nodes using IDynamicNodeProvider
- HtmlHelper Extensions
- Controlling URL Behavior
- Using Action Filter Attributes
- Sitemaps XML Protocol Endpoint for Search Engines
- Using Custom Attributes on a Node
- Specifying Node Order
- Advanced Node Visibility
- Multiple Navigation Paths to a Single Page
- [Multiple Sitemaps in One Application] (Multiple-Sitemaps-in-One-Application)
- [Security Trimming] (Security-Trimming)
[Version 3.x Documentation] (Version-3.x-Documentation)
Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.
- [MvcSiteMapProvider 4.0 - A Test Drive] (http://www.shiningtreasures.com/post/2013/08/07/MvcSiteMapProvider-40-a-test-drive)
- [MvcSiteMapProvider 4.0 - SEO Features Tutorial] (http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features)
- [How to Make MvcSiteMapProvider Remember a User’s Position] (http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position)
- [MvcSiteMapProvider 4.0 - Cache Configuration] (http://www.shiningtreasures.com/post/2013/08/11/mvcsitemapprovider-4-cache-configuration)
- [MvcSiteMapProvider 4.0 - Extending the Cache] (http://www.shiningtreasures.com/post/2013/08/13/mvcsitemapprovider-4-extending-the-cache)
- [MvcSiteMapProvider 4.0 - Unit Testing with the SiteMaps Static Methods] (http://www.shiningtreasures.com/post/2013/08/14/mvcsitemapprovider-4-unit-testing-with-the-sitemaps-static-methods)
- [Debugging an MvcSiteMapProvider Configuration] (http://www.shiningtreasures.com/post/2013/08/21/debugging-an-mvcsitemapprovider-configuration)
- [Converting from C# to Vb MvcSiteMapProvider] (http://www.developerfusion.com/thread/112710/converting-from-c-to-vb-mvcsitemapprovider/)
- [NightOwl888's MvcSiteMapProvider Demos] (https://github.com/NightOwl888?tab=repositories) - Filter for "MvcSiteMapProvider" to see the most relevant.
- [MvcSiteMapProvider Tutorial and Examples] (http://edspencer.me.uk/2011/02/10/mvc-sitemap-provider-tutorial/)
- [MvcSiteMapProvider Tutorial 2 - Breadcrumbs] (http://edspencer.me.uk/2011/09/20/mvc-sitemap-provider-tutorial-2-breadcrumbs/)
- [Getting Started with MvcSiteMapProvider] (http://blog.danstuken.com/2011/04/29/getting-started-with-mvcsitemapprovider/)
- [Inside the MvcSiteMapProvider - Part 1] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-1.html)
- [Inside the MvcSiteMapProvider - Part 2: Dynamic node providers] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-2.html)
- [Inside the MvcSiteMapProvider - Part 3: The ISiteMapVisibilityProvider] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-3.html)
- [Inside the MvcSiteMapProvider - Part 4: The IAclModule] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-4.html)
- [Inside the MvcSiteMapProvider - Part 5: The ISiteMapNodeUrlResolver] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-5.html)
- [Styling MvcSiteMapProvider with CSS] (http://tutsblog.net/styling-mvc-sitemap-provider-with-css/)
- [Using MvcSiteMapProvider with Twitter Bootstrap] (http://codingit.wordpress.com/2013/05/03/using-mvcsitemapprovider-with-twitter-bootstrap/)
- [ASP.NET MVC Menu using Site Map Provider & Bootstrap Navbar] (http://joeylicc.wordpress.com/2013/06/04/asp-net-mvc-menu-using-site-map-provider-bootstrap-navbar/)
- [StackOverflow MvcSiteMapProvider] (http://stackoverflow.com/questions/tagged/mvcsitemapprovider)
- [StackOverflow MvcSiteMap] (http://stackoverflow.com/questions/tagged/mvcsitemap)
- [StackOverflow ASP.NET MVC SiteMap] (http://stackoverflow.com/questions/tagged/asp.net-mvc-sitemap)
- [CodePlex Discussion Forum (no longer maintained)] (http://mvcsitemap.codeplex.com/discussions/topics/general?size=2147483647)
- [Telerik Forum] (http://www.telerik.com/search.aspx?insection=False&start=0&client=telerik_developer_tools&q=MvcSiteMapProvider&sid=1)
- [maartenba's blog] (http://blog.maartenballiauw.be/search.aspx?q=mvcsitemapprovider)
- [NightOwl888's blog] (http://www.shiningtreasures.com/category/MvcSiteMapProvider)