ParkSquare.AspNetCore.Sitemap 6.0.2

.NET 6.0
dotnet add package ParkSquare.AspNetCore.Sitemap --version 6.0.2
NuGet\Install-Package ParkSquare.AspNetCore.Sitemap -Version 6.0.2
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="ParkSquare.AspNetCore.Sitemap" Version="6.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ParkSquare.AspNetCore.Sitemap --version 6.0.2
#r "nuget: ParkSquare.AspNetCore.Sitemap, 6.0.2"
#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 ParkSquare.AspNetCore.Sitemap as a Cake Addin
#addin nuget:?package=ParkSquare.AspNetCore.Sitemap&version=6.0.2

// Install ParkSquare.AspNetCore.Sitemap as a Cake Tool
#tool nuget:?package=ParkSquare.AspNetCore.Sitemap&version=6.0.2

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

Build Status

Background

This package provides automatic generation of sitemap.xml and robots.txt for ASP.NET Core 6 applications. 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. The latest version of the package targets .Net 6.

Save the time and hassle of manually maintaining your sitemap files, by using the ASP.NET routing tables to generate a standards-compliant sitemap automatically. If you have routes you do not want to appear in your sitemap, simply decorate the Controller class or Action method with the exclusion attribute [SitemapExclude].

Extensibility

Asp.Net Core 6.0 Sitemap and Robots Generator is extensible. Out of the box, you will get URLs that are automatically discovered from your routing table. You can also create your own URL providers either to augment these generated URLs, or to replace them completely. For example, you can manually add URLs to your sitemap, including Video Sitemaps.

By default, requesting robots.txt will return you a basic file pointing to your generated site map. You can easily modify this behaviour by implementing your own robots provider classes.

Http or Https

By default, your sitemap URLs will use the same scheme (i.e. http or https) as the incoming request. In some circumstances, such as running behind nginx or a reverse proxy, the request may appear to your app as if it is http, but the end user sees https. In which case, you can use the 'force https' option to always use secure URLs in your sitemap.

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 add Sitemap to your application:

public void ConfigureServices(IServiceCollection services)
{
    // ... other code omitted ...

    services.AddSitemap();

    // ... other code omitted ...
}

This will register the Sitemap middleware, along with any custom URL providers and config.

Now add this line to Startup.cs to enable Sitemap:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ... other code omitted ...

    app.UseSitemap();

    // ... other code omitted ...
}

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();
    }
}

Customizing The Config

Default config is created with sensible defaults. If you wish to provide your own config, e.g. if you want to force https on all sitemap URLs, then create a class that implements ISitemapOptions. You do not need to register it with your DI container, this is done automatically for you.

public class MySitemapOptions : SitemapOptions
{
    public bool ForceHttps => true;
}

Create Custom Url Providers

By default, a Url Provider called RouteBasedSitemapProvider is registered, which uses the routing table to automatically figure out your site's URLs.

You can implement your own providers by creating classes that implement ISitemapUrlProvider or ISitemapVideoUrlProvider. Urls returned by ISitemapUrlProvider will be rendered in the conventional way. Urls returned by ISitemapVideoUrlProvider have more detail around video content and will be rendered as a Video Sitemap Element.

ISitemapUrlProvider

public class DemoSitemapUrlProvider : ISitemapUrlProvider
{
    public IReadOnlyCollection<ISitemapElement> GetElements(string baseUrl)
    {
        return new List<SitemapElement> {new SitemapElement($"{baseUrl}/demo/some-url")};
    }
}

ISitemapVideoUrlProvider

Note that ThumbnailUrl, Title, Description and VideoUrl are required and cannot be empty.

public class DemoSitemapVideoUrlProvider : ISitemapVideoUrlProvider
{
    public IReadOnlyCollection<ISitemapVideoElement> GetElements(string baseUrl)
    {
        return new List<ISitemapVideoElement>
        {
            new SitemapVideoElement($"{baseUrl}/video-element-demo")
            {
                // Required
                ThumbnailUrl = $"{baseUrl}/media/thumbnail.jpg",
                Title = "This is a demo video",
                Description = "A demonstration of how to add video elements to your sitemap",
                VideoUrl = $"{baseUrl}/media/video.mp4",
                // Optional
                DurationSeconds = 180,
                FamilyFriendly = true,
                Live = true,
                PublicationDate = DateTime.UtcNow,
                Rating = 4.9M,
                RequiresSubscription = true,
                ViewCount = 133041,
                Price = 12.99M,
                PriceCurrency = "EUR"
            }
        };
    }
}

By implementing your own providers, you can get URLs for your sitemap from any data source, for example CRM system, JSON file, XML file, SQL database, or even manually hardcode them.

Create Custom Robots.txt Providers

By default, a Robots Provider called DefaultRobotsProvider is registered, which returns the following basic file, pointing to your generated site map:

User-agent: *
Allow: /
Sitemap: <YOUR SITEMAP URL> 

You can implement your own providers by creating classes that implement IRobotsProvider. You can remove the default provider from your DI container if you wish. Providers will be asked for their text entry to the returned robots.txt file in the order they are registered.

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 was computed.  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. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.2 395 8/15/2023
6.0.1 1,256 6/9/2023
1.2.67 99 6/8/2023
1.2.66 1,366 9/11/2022
1.2.65 368 8/22/2022
1.2.63 349 8/19/2022
1.2.62 472 11/22/2021
1.2.59 710 5/16/2021
1.2.56 426 4/17/2021
1.0.1 781 4/17/2020
1.0.0 583 4/17/2020