SimpleMvcSitemap.Core 4.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleMvcSitemap.Core --version 4.3.0
NuGet\Install-Package SimpleMvcSitemap.Core -Version 4.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleMvcSitemap.Core" Version="4.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleMvcSitemap.Core --version 4.3.0
#r "nuget: SimpleMvcSitemap.Core, 4.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleMvcSitemap.Core as a Cake Addin
#addin nuget:?package=SimpleMvcSitemap.Core&version=4.3.0

// Install SimpleMvcSitemap.Core as a Cake Tool
#tool nuget:?package=SimpleMvcSitemap.Core&version=4.3.0

SimpleMvcSitemap

A minimalist library for creating sitemap files inside ASP.NET Core applications.

SimpleMvcSitemap lets you create sitemap files inside action methods without any configuration. It also supports generating sitemap index files. Since you are using regular action methods you can take advantage of caching and routing available in the framework.

Table of contents

<a id="requirements">Requirements</a>

  • ASP.NET Core 6 and newer

<a id="installation">Installation</a>

Install the NuGet package on your MVC project.

Install-Package SimpleMvcSitemap.Core

Add to DI Container

public class Startup
{
    // ...
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.AddSingleton<ISitemapProvider, SitemapProvider>();
        // ...
    }
    // ...
}

.NET Framework

Support for .NET Framework and ASP.NET MVC has been dropped by version 4 of original SimpleMvcSitemap. Use version 3 if you need to support this scenario.

<a id="examples">Examples</a>

You can use SitemapProvider class to create sitemap files inside any action method. You don't even have to provide absolute URLs, SimpleMvcSitemap can generate them from relative URLs. Here's an example:

public class SitemapController : Controller
{
    private readonly ISitemapProvider _sitemapProvider;

    public SitemapController(ISitemapProvider sitemapProvider)
    {
        _sitemapProvider = sitemapProvider;
    }

    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>
        {
            new SitemapNode(Url.Action("Index","Home")),
            new SitemapNode(Url.Action("About","Home")),
            //other nodes
        };

        return _sitemapProvider.CreateSitemap(new SitemapModel(nodes));
    }
}

SitemapNode class also lets you specify the optional attributes:

new SitemapNode(Url.Action("Index", "Home"))
{
    ChangeFrequency = ChangeFrequency.Weekly,
    LastModificationDate = DateTime.UtcNow.ToLocalTime(),
    Priority = 0.8M
}

<a id="sitemap-index-files">Sitemap Index Files</a>

Sitemap files must have no more than 50,000 URLs and must be no larger then 50MB as stated in the protocol. If you think your sitemap file can exceed these limits you should create a sitemap index file. If you have a logical seperation, you can create an index manually.

List<SitemapIndexNode> sitemapIndexNodes = new List<SitemapIndexNode>
{
   new SitemapIndexNode(Url.Action("Categories","Sitemap")),
   new SitemapIndexNode(Url.Action("Products","Sitemap"))
};

return new SitemapProvider().CreateSitemapIndex(new SitemapIndexModel(sitemapIndexNodes));

If you are dealing with dynamic data and you are retrieving the data using a LINQ provider, SimpleMvcSitemap can handle the paging for you. A regular sitemap will be created if you don't have more nodes than the sitemap size.

Generating sitemap index files

This requires a little configuration:

public class ProductSitemapIndexConfiguration : SitemapIndexConfiguration<Product>
{
    private readonly IUrlHelper urlHelper;

    public ProductSitemapIndexConfiguration(IQueryable<Product> dataSource, int? currentPage, IUrlHelper urlHelper)
        : base(dataSource,currentPage)
    {
        this.urlHelper = urlHelper;
    }

    public override SitemapIndexNode CreateSitemapIndexNode(int currentPage)
    {
        return new SitemapIndexNode(urlHelper.RouteUrl("ProductSitemap", new { currentPage }));
    }

    public override SitemapNode CreateNode(Product source)
    {
        return new SitemapNode(urlHelper.RouteUrl("Product", new { id = source.Id }));
    }
}

Then you can create the sitemap file or the index file within a single action method.

public ActionResult Products(int? currentPage)
{
    var dataSource = products.Where(item => item.Status == ProductStatus.Active);
    var productSitemapIndexConfiguration = new ProductSitemapIndexConfiguration(dataSource, currentPage, Url);
    return new DynamicSitemapIndexProvider().CreateSitemapIndex(new SitemapProvider(), productSitemapIndexConfiguration);
}

<a id="datetime-format">DateTime Format</a>

You should convert your DateTime values to local time. Universal time format generated by .NET is not accepted by Google. You can use .ToLocalTime() method to do the conversion.

<a id="google-sitemap-extensions">Google Sitemap Extensions</a>

You can use Google's sitemap extensions to provide detailed information about specific content types like images, videos, news or alternate language pages. You can still use relative URLs for any of the additional URLs.

<a id="images">Images</a>

using SimpleMvcSitemap.Images;

new SitemapNode(Url.Action("Display", "Product"))
{
    Images = new List<SitemapImage>
    {
        new SitemapImage(Url.Action("Image","Product", new {id = 1})),
        new SitemapImage(Url.Action("Image","Product", new {id = 2}))
    }
}

<a id="videos">Videos</a>

By version 4, multiple videos are supported. Start using Videos property if you are upgrading from v3 to v4.

using SimpleMvcSitemap.Videos;

new SitemapNode("http://www.example.com/videos/some_video_landing_page.html")
{
    Videos = new List<SitemapVideo>
    { 
        new SitemapVideo(title: "Grilling steaks for summer",
                         description: "Alkis shows you how to get perfectly done steaks every time",
                         thumbnailUrl: "http://www.example.com/thumbs/123.jpg", 
                         contentUrl: "http://www.example.com/video123.flv")
    }
}

<a id="news">News</a>

using SimpleMvcSitemap.News;

new SitemapNode("http://www.example.org/business/article55.html")
{
    News = new SitemapNews(newsPublication: new NewsPublication(name: "The Example Times", language: "en"),
                           publicationDate: new DateTime(2014, 11, 5, 0, 0, 0, DateTimeKind.Utc),
                           title: "Companies A, B in Merger Talks")
}

<a id="translations">Alternate language pages</a>

using SimpleMvcSitemap.Translations;

new SitemapNode("abc")
{
    Translations = new List<SitemapPageTranslation>
    {
        new SitemapPageTranslation("http://www.example.com/deutsch/", "de"),
		new SitemapPageTranslation("http://www.example.com/english/", "en")
    }
}

<a id="style-sheets">XSL Style Sheets</a>

SimpleMvcSitemap supports XSL style sheets by version 3. Keep in mind that XML stylesheets are subjected to the same origin checks.

using SimpleMvcSitemap.StyleSheets;

new SitemapModel(new List<SitemapNode> { new SitemapNode("abc") })
{
    StyleSheets = new List<XmlStyleSheet>
    {
        new XmlStyleSheet("/sitemap.xsl")
    }
};

You can see how you can utilize multiple XSL style sheets in this tutorial.

<a id="base-url">Custom Base URL</a>

SimpleMvcSitemap can generate absolute URLs from the relative URLs using the HTTP request context. If you want to customize this behaviour, you can implement IBaseUrlProvider interface and pass it to the SitemapProvider class.

public class BaseUrlProvider : IBaseUrlProvider
{
    public Uri BaseUrl => new Uri("http://example.com");
}

var sitemapProvider = new SitemapProvider(new BaseUrlProvider());

<a id="di">Unit Testing and Dependency Injection</a>

SitemapProvider class implements the ISitemapProvider interface which can be injected to your controllers and be replaced with test doubles. All methods are thread safe so they can be used with singleton life cycle.

public class SitemapController : Controller
{
    private readonly ISitemapProvider _sitemapProvider;

    public SitemapController(ISitemapProvider sitemapProvider)
    {
        _sitemapProvider = sitemapProvider;
    }
	
    //action methods
}

<a id="autogenerate">Sitemap Automatic Creation</a>

As of version 4.3.0 you can now auto-generate sitemap.xml (and optionally a robot.txt) file by having the system auto discover all controllers and actions (and/or razor pages).

This will inject a middleware that will listen for HTTP request to \sitemap.xml, and optionally \robots.txt. When that occurs it will auto-respond to the request by outputing an automatically generated sitemap.xml or robots.txt

Here are some examples of what you would add to your startup code:

Example of Sitemap generation

app.UseSitemap(new SitemapGeneratorOptions() { DefaultChangeFrequency = SimpleMvcSitemap.ChangeFrequency.Daily, DefaultPriority = .9M, LastModifiedDate = File.GetCreationTime(Assembly.GetExecutingAssembly().Location) });

Example of Sitemap Index generation

app.UseSitemap(new SitemapGeneratorOptions() {  DefaultSiteMapType = SiteMapType.SitemapIndex });

Example of Sitemap Index generation with optional robots.txt creation as well

app.UseSitemap(new SitemapGeneratorOptions() {  EnableRobotsTxtGeneration = true });

<a id="license">License</a>

SimpleMvcSitemap is licensed under MIT License. Refer to license file for more information.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SimpleMvcSitemap.Core:

Package Downloads
Panda.Tools

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.4.0 457 3/31/2024
4.3.0 15,698 6/9/2023
4.2.0 32,257 11/14/2021
4.1.0 2,601 9/27/2021
4.0.3 14,264 7/24/2020
4.0.0 1,940 7/22/2020