Casko.AspNetCore.XmlSiteMaps 2.0.5

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

// Install Casko.AspNetCore.XmlSiteMaps as a Cake Tool
#tool nuget:?package=Casko.AspNetCore.XmlSiteMaps&version=2.0.5                

XML Sitemaps

Zero config XML sitemaps. A thing for creating XML sitemaps in AspNetCore applications.

Get started

Follow these instructions to get started.

Install

dotnet add package Casko.AspNetCore.XmlSiteMaps --version 1.0.0

Configure

In Startup.cs or Program.cs, add XML sitemaps on IServiceCollection:

services.AddXmlSiteMaps();

In Startup.cs or Program.cs, use XML sitemaps on IApplicationBuilder:

app.UseXmlSiteMaps(addRewrites: true); // true is default

If ordering creates problems for rewrites then set useRewrites: false and then call where it fit in:

app.UseXmlSiteMapsRewrites();

After configuring XmlSiteMaps, then implementations of IXmlSiteMap<XmlSiteMapIndex>, IXmlSiteMap<XmlSiteMap>, IXmlSiteMapCollection<XmlSiteMap> and IXmlSiteMapCollection<XmlSiteMapIndex> will be picked up on application startup and endpoints and rewrites will be setup.

XML sitemaps will be served from api/xml-sitemaps/... and URL rewrites will be created and point FileName to it's corresponding endpoint.

Implement

Example code for creating different XML sitemap things.

Create a single XML sitemap:

Simplest for of XML sitemap.

public class XmlSiteMapService : IXmlSiteMap<XmlSiteMap>
{
    public string FileName => "sitemap.xml";

    public XmlSiteMap GetXmlSiteMap()
    {
        var xmlSiteMap = new XmlSiteMap();
        
        xmlSiteMap.Urls.Add(new XmlSiteMapUrl()
        {
            Location = "https://...",
            LastModified = DateTime.UtcNow,
            ChangeFrequency = ChangeFrequency.Daily,
            Priority = .5
        });

        ...

        return xmlSiteMap;
    }
}
Create a XML sitemap index:

When multiple XML sitemaps exist you will need at least one XML sitemap index.

public class XmlSiteMapIndexService : IXmlSiteMap<XmlSiteMapIndex>
{
    public string FileName => "sitemap.xml";

    public XmlSiteMapIndex GetXmlSiteMap()
    {
        var xmlSiteMapIndex = new XmlSiteMapIndex();
        
        xmlSiteMap.Locations.Add(new XmlSiteMapIndexLocation()
        {
            Location = "https://...",
        });

        ...

        return xmlSiteMapIndex;
    }
}
Create XML sitemap collection:

Create many XML sitemaps with a single implementation.

public class XmlSiteMapCollectionService() : IXmlSiteMapCollection<XmlSiteMap>
{
    public IDictionary<string, string> Routes => new Dictionary<string, string>()
    {
        { "en", "sitemap-collection-default.xml" },
        { "da", "sitemap-collection-da.xml" },
        { "es", "sitemap-collection-es.xml" },
    };

    public XmlSiteMap GetXmlSiteMap(string key)
    {
        var xmlSiteMap = new XmlSiteMap();

        ...Create things using "key"
        
        xmlSiteMap.Urls.Add(new XmlSiteMapUrl()
        {
            Location = $"https://...{key}",
            LastModified = DateTime.UtcNow,
            ChangeFrequency = ChangeFrequency.Daily,
            Priority = .5
        });
        
        return xmlSiteMap;
    }
}

Changelog

2.0.0

  • Add HttpContext to signature of ISiteMap.GetXmlSiteMap
  • Pass HttpContext when building endpoints

1.0.1

  • Remove required operator from Rel property of XHtmlLink

1.0.0

  • Initialize project

TODO

  • Offer automatic XML sitemap index creation when creating IXmlSitemapCollection<XmlSiteMap>
  • Make XHtmlLink not required
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
2.0.5 80 9/22/2024
2.0.3 87 8/30/2024
2.0.1 85 8/30/2024
2.0.0 98 8/27/2024
1.0.1 95 8/26/2024
1.0.0 114 8/25/2024