Asp.Net Core Sitemap Generator

ParkSquare.AspNetCore.Sitemap

BUILD STATUS Continuous Integration Build Status

INSTALLATION

Install-Package ParkSquare.AspNetCore.Sitemap

DESCRIPTION

Automatic generation of sitemap.xml and robots.txt for ASP.NET Core application. Uses routing tables to generate a standards compliant sitemap file. Includes HTTPs override, and controller or action level exclusions.

Documentation

This Sitemap middleware works by looking at all controller routes for valid pages that should be included in the sitemap. 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 latter will contain a link directing robots (e.g. Googlebot) to your sitemap.

How To Use

Install the Nuget package and add this line to Startup.cs:


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

If you have any controllers or actions you do not want to be included in the sitemap, decorate them 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();
        }
    }

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

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

Reverse Proxies

If you are behind a reverse proxy of some sort, such as Nginx when running containerized applications in Docker, your site URL will by default be that of the 'internal' site. This will probably be incorrect. To resolve this, install the Microsoft.AspNetCore.HttpOverrides Nuget package which provides middleware to override various values in the request object to reflect the 'real' request.


    app.UseMiddleware<ForwardedHeadersMiddleware>();
    app.UseSitemap();

SSL Offloading

If you are offloading SSL then the scheme may still be incorrect even after doings this. If so, you can force all URLs to be secure by passing some settings into the constructor:


    app.UseMiddleware<ForwardedHeadersMiddleware>();
    app.UseSitemap(new SitemapOptions {ForceHttps = true});