MintPlayer.AspNetCore.SitemapXml 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MintPlayer.AspNetCore.SitemapXml --version 1.0.0
NuGet\Install-Package MintPlayer.AspNetCore.SitemapXml -Version 1.0.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="MintPlayer.AspNetCore.SitemapXml" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MintPlayer.AspNetCore.SitemapXml --version 1.0.0
#r "nuget: MintPlayer.AspNetCore.SitemapXml, 1.0.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 MintPlayer.AspNetCore.SitemapXml as a Cake Addin
#addin nuget:?package=MintPlayer.AspNetCore.SitemapXml&version=1.0.0

// Install MintPlayer.AspNetCore.SitemapXml as a Cake Tool
#tool nuget:?package=MintPlayer.AspNetCore.SitemapXml&version=1.0.0

MintPlayer.AspNetCore.SitemapXml

Helper library to host a sitemap from your ASP.NET Core application

NuGet package

https://www.nuget.org/packages/MintPlayer.AspNetCore.SitemapXml/

Installation

NuGet package manager

Open the NuGet package manager and install MintPlayer.AspNetCore.SitemapXml in your project

Package manager console

Install-Package MintPlayer.AspNetCore.SitemapXml

Usage

Adding SitemapXML services

Add the SitemapXML services (Startup@ConfigureServices).

services.AddSitemapXml();

This call makes the ISitemapXml service available as a scoped service.

Enable XML formatters

Modify Startup@ConfigureServices

services.AddMvc(options => {
    // Optionally, for content-negotiation
    options.RespectBrowserAcceptHeader = true; // false by default
})
.AddXmlSerializerFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Add SitemapController

An example of your SitemapController. Notice the use of the Produces attribute:

[Controller]
[Route("[controller]")]
public class SitemapController : Controller
{
    private ISitemapXml sitemapXml;
    private IPersonRepository personRepository;
    public SitemapController(ISitemapXml sitemapXml, IPersonRepository personRepository)
    {
        this.sitemapXml = sitemapXml;
        this.personRepository = personRepository;
    }

    [Produces("application/xml")]
    [HttpGet(Name = "sitemap-index")]
    public SitemapIndex Index()
    {
        const int per_page = 100;

        var people = personRepository.GetPeople().ToList();
        var person_urls = sitemapXml.GetSitemapIndex(people, per_page, (perPage, page) => Url.RouteUrl("sitemap-person", new { count = perPage, page }, Request.Scheme));
        
        return new SitemapIndex(person_urls);
    }

    [Produces("application/xml")]
    [HttpGet("{count}/{page}", Name = "sitemap")]
    public UrlSet Sitemap(int count, int page)
    {
        var people = personRepository.GetPeople(count, page);
        return new UrlSet(people.Select(p => {
            var url = new Url {
                Loc = $"{Request.Scheme}://{Request.Host}/person/{p.Id}",
                ChangeFreq = SitemapXml.Enums.ChangeFreq.Monthly,
                LastMod = p.DateUpdate,
            };
            url.Links.Add(new Link {
                Rel = "alternate",
                HrefLang = "nl",
                Href = $"{Request.Scheme}://{Request.Host}/person/{p.Id}?lang=nl"
            });
            url.Links.Add(new Link {
                Rel = "alternate",
                HrefLang = "fr",
                Href =  $"{Request.Scheme}://{Request.Host}/person/{p.Id}?lang=fr"
            });
            return url;
        }));
    }
}

Styling your sitemap

You can use an XSL stylesheet for your sitemaps. Modify Startup@ConfigureServices

services.AddMvc(options => {
    options.RespectBrowserAcceptHeader = true; // false by default
})
.AddXmlSerializerFormatters()
.AddSitemapXmlFormatters(options => {
    options.StylesheetUrl = "/assets/sitemap.xsl";
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Now you can either put your own XSLT file in the ClientApp/src/assets folder, or call the UseDefaultSitemapXmlStylesheet middleware.

Using the built-in XML stylesheet

Put the following middleware before the app.UseMvc call:

app.UseDefaultSitemapXmlStylesheet(options =>
{
    options.StylesheetUrl = "/assets/sitemap.xsl";
});

Now an XML Stylesheet is hosted on the specified URL. You no longer need to put a sitemap.xsl in the assets folder.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  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. 
.NET Core netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.0.0 204 11/14/2023
7.0.0 349 11/18/2022
7.0.0-preview.1 85 11/4/2022
6.0.3 342 11/4/2022
6.0.2 344 12/26/2021
6.0.1 329 12/26/2021
2.0.2 320 8/18/2021
2.0.1 377 6/27/2021
2.0.0 371 6/27/2021
1.2.2 387 8/20/2020
1.2.1 446 8/13/2020
1.2.0 475 4/27/2020
1.1.4 444 4/27/2020
1.1.3 424 4/27/2020
1.1.2 427 4/27/2020
1.1.1 433 4/22/2020
1.1.0 397 4/22/2020
1.0.7 410 4/22/2020
1.0.6 421 4/20/2020
1.0.5 448 2/3/2020
1.0.4 429 2/3/2020
1.0.3 442 1/13/2020
1.0.2 480 1/13/2020
1.0.1 511 8/29/2019
1.0.0 502 8/29/2019