MvcSiteMapProvider Documentation

repository·master·Indexed 19 days ago

https://github.com/maartenba/mvcsitemapprovider

A flexible tool for ASP.NET MVC that manages hierarchical navigation structures, including menus, breadcrumbs, and SEO-friendly sitemaps. It uses dynamic routing based on controller and action methods rather than static URLs. The library supports MVC versions 2 through 5 and provides a pluggable architecture for extensibility, including integration guides for dependency injection containers such as Autofac, Grace DI, and Ninject.

Tokens
6K
Snippets
11
Records
21
Agent score
68%

What's inside MvcSiteMapProvider

  1. Core concepts of MvcSiteMapProvider

    master

    MvcSiteMapProvider is a tool for ASP.NET MVC that provides flexible menus, breadcrumb trails, and SEO features.

    Key Features:

    • Hierarchical Navigation: Configure site maps using XML, database, or code-driven approaches.
    • Dynamic Routing: Sitemap nodes are based on controller and action method names rather than hardcoded URLs, making them compatible with the application's routing engine.
    • SEO Support: Automatically provides dynamic sitemaps XML, canonical URL tags, and meta robots tags.
    • Extensibility: Features a pluggable architecture with interface-based extensibility points, allowing you to replace virtually any part of the provider (e.g., caching, dependency injection, or node providers).
    • Advanced Capabilities: Supports multi-tenant applications and flexible caching.
  2. Install MvcSiteMapProvider via NuGet

    master

    Install the version of MvcSiteMapProvider that matches your ASP.NET MVC version using the NuGet Package Manager Console.

    Stable Releases:

    • For MVC 5: Install-Package MvcSiteMapProvider.MVC5
    • For MVC 4: Install-Package MvcSiteMapProvider.MVC4
    • For MVC 3: Install-Package MvcSiteMapProvider.MVC3
    • For MVC 2: Install-Package MvcSiteMapProvider.MVC2

    Continuous Integration (Prerelease) Builds: If you require builds from the MyGet continuous integration feed, use the -IncludePrerelease flag and specify the source:

    • For MVC 5: Install-Package MvcSiteMapProvider.MVC5 -IncludePrerelease -Source http://www.myget.org/F/mvcsitemapprovider
    • For MVC 4: Install-Package MvcSiteMapProvider.MVC4 -IncludePrerelease -Source http://www.myget.org/F/mvcsitemapprovider
    • For MVC 3: Install-Package MvcSiteMapProvider.MVC3 -IncludePrerelease -Source http://www.myget.org/F/mvcsitemapprovider
    • For MVC 2: Install-Package MvcSiteMapProvider.MVC2 -IncludePrerelease -Source http://www.myget.org/F/mvcsitemapprovider

    Additionally, you can use specific dependency injection packages available on NuGet to integrate with your DI container.

    Install-Package MvcSiteMapProvider.MVC5
  3. Manage MvcSiteMapProvider package versions

    master

    The MvcSiteMapProvider package is considered obsolete because NuGet cannot support multiple ASP.NET MVC versions within a single package. Instead, MvcSiteMapProvider provides a package per supported MVC version.

    If you are using the generic MvcSiteMapProvider package, it targets the latest supported MVC version and may force an unwanted MVC upgrade during updates. It is recommended to switch to a version-specific package (e.g., MvcSiteMapProvider.MVC5) to ensure stability.

  4. Maintain DI configuration during upgrades

    master

    Because MvcSiteMapProvider uses DI to allow extensibility, upgrading the package may introduce new requirements in your DI configuration. Since NuGet cannot automatically merge changes into your custom DI modules, you must manually manage updates.

    Upgrade Workflow:

    1. When upgrading MvcSiteMapProvider, do not simply overwrite your existing DI configuration.
    2. Compare your current DI module/registry against the latest version in the official repository using a diff tool (e.g., Beyond Compare).
    3. Identify any new registrations or configuration changes required by the new version.
    4. Merge these changes into your configuration without overwriting your existing customizations.
    5. Focus on the code relevant to your specific .NET and MVC version (ignore #if preprocessor directive blocks themselves).

    The latest StructureMap registry can be found in the repository's master branch under src/MvcSiteMapProvider/CodeAsConfiguration/StructureMap/DI/StructureMap/Registries/MvcSiteMapProviderRegistry.cs.

  5. Integrate MvcSiteMapProvider with StructureMap

    master

    To integrate MvcSiteMapProvider into your Dependency Injection (DI) setup using StructureMap, you must add the provider's registry to your container and assign the ISiteMapLoader to the global MvcSiteMapProvider.SiteMaps.Loader property.

    Follow these steps in your composition root:

    1. Register the Provider: Add MvcSiteMapProviderRegistry to your StructureMap configuration.
    2. Configure the Loader (Required): Assign the ISiteMapLoader instance from your container to MvcSiteMapProvider.SiteMaps.Loader.
    3. Validate XML (Optional): Use ISiteMapXmlValidator to ensure your .sitemap files adhere to the required XSD.
    4. Register Routes (Optional): Call XmlSiteMapController.RegisterRoutes to make your sitemaps accessible to search engines.
    // Create the DI container (typically part of your DI setup already)
    var container = new StructureMapContainer();
    
    // Setup configuration of DI (required)
    container.Configure(r => r.AddRegistry<MvcSiteMapProviderRegistry>());
    
    // Setup global sitemap loader (required)
    MvcSiteMapProvider.SiteMaps.Loader = container.GetInstance<ISiteMapLoader>();
    
    // Check all configured .sitemap files to ensure they follow the XSD for MvcSiteMapProvider (optional)
    var validator = container.GetInstance<ISiteMapXmlValidator>();
    validator.ValidateXml(HostingEnvironment.MapPath("~/Mvc.sitemap"));
    
    // Register the Sitemaps routes for search engines (optional)
    XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
  6. Maintain Grace DI configuration during upgrades

    master

    When upgrading MvcSiteMapProvider, your existing DI configuration might break if new features require changes to the MvcSiteMapProviderModule. Because NuGet cannot automatically merge changes into your custom DI modules, you must manually verify your configuration.

    Upgrade Workflow:

    1. Upgrade the MvcSiteMapProvider NuGet package.
    2. Locate the latest version of MvcSiteMapProviderModule.cs in the project's master branch.
    3. Use a diff tool (e.g., Beyond Compare) to compare your local module with the new version.
    4. Manually merge any new configuration logic into your module without overwriting your existing customizations.

    Note: You do not need to merge preprocessor directives (#if, #else, #endif), only the logic within the blocks that applies to your specific .NET or MVC version.

  7. Reinstall a specific version of MvcSiteMapProvider for a target MVC version

    master

    If you need to target a specific version of ASP.NET MVC, you should remove the obsolete package and all its dependencies, then install the version-specific package.

    1. Remove the obsolete package and dependencies:
    Uninstall-Package MvcSiteMapProvider -RemoveDependencies
    1. Install the appropriate version-specific package (replace [x] with your target MVC version, e.g., MvcSiteMapProvider.MVC5):
    Install-Package MvcSiteMapProvider.MVC[x]
    Uninstall-Package MvcSiteMapProvider -RemoveDependencies
    Install-Package MvcSiteMapProvider.MVC[x]
  8. Build the MvcSiteMapProvider source code

    master

    To build the project from the cloned repository, run build.cmd.

    The project uses the psake build engine (a PowerShell-based engine). If you have never executed PowerShell scripts on your system before, you may need to allow script execution by running the following command as an administrator:

    Set-ExecutionPolicy RemoteSigned

    Set-ExecutionPolicy RemoteSigned
  9. Integrate MvcSiteMapProvider with Unity DI

    master

    To integrate MvcSiteMapProvider into your Unity Dependency Injection (DI) configuration, you must add the MvcSiteMapProviderContainerExtension to your container and configure the global sitemap loader.

    Follow these steps in your composition root:

    1. Add the extension module: Use container.AddNewExtension<MvcSiteMapProviderContainerExtension>(). This is required.
    2. Setup the global sitemap loader: Assign MvcSiteMapProvider.SiteMaps.Loader by resolving ISiteMapLoader from your container. This is required.
    3. Validate XML (Optional): Resolve ISiteMapXmlValidator and call ValidateXml on your .sitemap file path to ensure it follows the MvcSiteMapProvider XSD.
    4. Register routes (Optional): Call XmlSiteMapController.RegisterRoutes(RouteTable.Routes) to expose sitemap routes to search engines.
    // Create the container (typically part of your DI setup already)
    var container = new UnityContainer();
    
    // Add the extension module (required)
    container.AddNewExtension<MvcSiteMapProviderContainerExtension>();
    
    // Setup global sitemap loader (required)
    MvcSiteMapProvider.SiteMaps.Loader = container.Resolve<ISiteMapLoader>();
    
    // Check all configured .sitemap files to ensure they follow the XSD for MvcSiteMapProvider (optional)
    var validator = container.Resolve<ISiteMapXmlValidator>();
    validator.ValidateXml(HostingEnvironment.MapPath("~/Mvc.sitemap"));
    
    // Register the Sitemaps routes for search engines (optional)
    XmlSiteMapController.RegisterRoutes(RouteTable.Routes);
  10. Integrate MvcSiteMapProvider with Autofac

    master

    To use MvcSiteMapProvider with Autofac, you must register the required modules in your composition root and assign the resolved ISiteMapLoader to the MvcSiteMapProvider.SiteMaps.Loader property.

    Integration Steps

    1. Register Modules: Add MvcSiteMapProviderModule and MvcModule to your ContainerBuilder.
    2. Assign Loader: After building the container, resolve ISiteMapLoader and assign it to MvcSiteMapProvider.SiteMaps.Loader.
    3. Optional - Validate XML: Resolve ISiteMapXmlValidator to ensure your .sitemap files adhere to the XSD.
    4. Optional - Register Routes: Call XmlSiteMapController.RegisterRoutes(RouteTable.Routes) to expose sitemaps to search engines.
    // 1. Register modules
    var builder = new ContainerBuilder();
    builder.RegisterModule(new MvcSiteMapProviderModule()); // Required
    builder.RegisterModule(new MvcModule()); // Required by MVC
    
    // 2. Create the DI container
    var container = builder.Build();
    
    // 3. Setup global sitemap loader (required)
    MvcSiteMapProvider.SiteMaps.Loader = container.Resolve<ISiteMapLoader>();
    
    // 4. Optional: Validate XML
    var validator = container.Resolve<ISiteMapXmlValidator>();
    validator.ValidateXml(HostingEnvironment.MapPath("~/Mvc.sitemap"));
    
    // 5. Optional: Register Sitemaps routes
    XmlSiteMapController.RegisterRoutes(RouteTable.Routes);