Last Updated: 22 November 2021

Nuget Nuget
Explore code with Fuget
Install-Package ParkSquare.AspNetCore.Sitemap

Asp.Net Core Sitemap.xml & Robots.txt Generator

Build Status

Background

This package provides automatic generation of sitemap.xml and robots.txt for ASP.NET Core application. Both of these files are considered essential for good Search Engine Optimization (SEO) and are looked upon favourably by search engines such as Google and Bing.

Save the time and hassle of manually maintaining these files, by using the ASP.NET routing tables to generate a standards-compliant sitemap file. Has configurable options to force HTTPs (all sitemap links will use the https protocol), plus Controller or Action exclusions.

Getting Started

Sitemap is implemented as ASP.NET middleware, and works by looking at all controller routes for valid pages that should be included in the xml output. By default this will only include GET verbs. If no explicit attribute is defined on your controller method, it will be assumed to be a GET.

Any requests to /sitemap.xml or /robots.txt will be handled by the Sitemap middleware. The robots file will contain a link directing robots (e.g. Googlebot) to your sitemap.

Once you have installed the Nuget package, add this line to Startup.cs to enable Sitemap:

// Place just above app.UseMvc(..)
app.UseSitemap();

Excluding Controller From Sitemap

To exclude a controller and all actions within it, such as an error or login controller, decorate the controller class with the [SitemapExclude] attribute:

// All routes in this controller will be ignored
[SitemapExclude]
public class BlahController : Controller
{
    [Route("some-route")]
    public IActionResult Something()
    {
        return View();
    }
}

Excluding Specific Actions/Routes From Sitemap

To exclude only certain actions, routes, or pages from your sitemap, decorate the action method with the [SitemapExclude] attribute:

public class BlahController : Controller
{
    [SitemapExclude]
    [Route("some-route")]
    public IActionResult Ignored()
    {
        return View();
    }

    [Route("some-other-route")]
    public IActionResult NotIgnored()
    {  
        return View();
    }
}