MintPlayer.AspNetCore.SitemapXml 1.2.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.2.0                
NuGet\Install-Package MintPlayer.AspNetCore.SitemapXml -Version 1.2.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.2.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.2.0                
#r "nuget: MintPlayer.AspNetCore.SitemapXml, 1.2.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.2.0

// Install MintPlayer.AspNetCore.SitemapXml as a Cake Tool
#tool nuget:?package=MintPlayer.AspNetCore.SitemapXml&version=1.2.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
    .AddControllersWithViews(options => {
        options.RespectBrowserAcceptHeader = true;
    })
    .AddXmlSerializerFormatters()
    .AddMvcOptions(mvc_options => {
        mvc_options.OutputFormatters.Insert(0, new Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter());
    })
    .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);

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
    .AddControllersWithViews(options => {
        options.RespectBrowserAcceptHeader = true;
    })
    .AddXmlSerializerFormatters()
    .AddMvcOptions(mvc_options => {
        mvc_options.OutputFormatters.Insert(0, new Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter());
    })
    .AddSitemapXmlFormatters(options => {
        options.StylesheetUrl = "/assets/sitemap.xsl";
    })
    .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);

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 netcoreapp3.1 is compatible. 
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
9.1.2 66 11/17/2024
9.1.1 70 11/17/2024
9.1.0 70 11/17/2024
9.0.0 69 11/13/2024
8.0.0 279 11/14/2023
7.0.0 364 11/18/2022
7.0.0-preview.1 97 11/4/2022
6.0.3 365 11/4/2022
6.0.2 377 12/26/2021
6.0.1 353 12/26/2021
2.0.2 355 8/18/2021
2.0.1 407 6/27/2021
2.0.0 383 6/27/2021
1.2.2 412 8/20/2020
1.2.1 473 8/13/2020
1.2.0 504 4/27/2020
1.1.4 471 4/27/2020
1.1.3 447 4/27/2020
1.1.2 451 4/27/2020
1.1.1 461 4/22/2020
1.1.0 423 4/22/2020
1.0.7 436 4/22/2020
1.0.6 430 4/20/2020
1.0.5 473 2/3/2020
1.0.4 451 2/3/2020
1.0.3 466 1/13/2020
1.0.2 502 1/13/2020
1.0.1 538 8/29/2019
1.0.0 512 8/29/2019