Sidio.Sitemap.AspNetCore
3.1.0
Prefix Reserved
dotnet add package Sidio.Sitemap.AspNetCore --version 3.1.0
NuGet\Install-Package Sidio.Sitemap.AspNetCore -Version 3.1.0
<PackageReference Include="Sidio.Sitemap.AspNetCore" Version="3.1.0" />
<PackageVersion Include="Sidio.Sitemap.AspNetCore" Version="3.1.0" />
<PackageReference Include="Sidio.Sitemap.AspNetCore" />
paket add Sidio.Sitemap.AspNetCore --version 3.1.0
#r "nuget: Sidio.Sitemap.AspNetCore, 3.1.0"
#:package Sidio.Sitemap.AspNetCore@3.1.0
#addin nuget:?package=Sidio.Sitemap.AspNetCore&version=3.1.0
#tool nuget:?package=Sidio.Sitemap.AspNetCore&version=3.1.0
Sidio.Sitemap.AspNetCore
Sidio.Sitemap.AspNetCore is a lightweight .NET library for generating sitemaps and a sitemap index in ASP .NET Core applications.
In addition to sitemap and sitemap index generation, news, images and video extensions are also supported.
Versions
| Sidio.Sitemap.Core | Sidio.Sitemap.AspNetCore | Sidio.Sitemap.Blazor | |
|---|---|---|---|
| NuGet | |||
| Build | |||
| Coverage | |||
| Requirements | .NET Standard, .NET 8+, | .NET 8+, AspNetCore | .NET 8+, AspNetCore, Blazor server |
Installation
Add the package to your project.
Usage
There are two ways to generate sitemaps: manually or by using middleware. When using middleware, the sitemap is generated automatically.
Building sitemaps manually
Sitemap
// di setup
services.AddHttpContextAccessor();
services.AddDefaultSitemapServices<HttpContextBaseUrlProvider>();
// controller
[HttpGet]
public IActionResult Sitemap()
{
var nodes = new List<SitemapNode> { new ("page.html"), SitemapNode.Create(Url.Action("Index")) };
var sitemap = new Sitemap(nodes);
return new SitemapResult(sitemap);
}
Sitemap and sitemap index
[Route("sitemap.xml")]
public IActionResult SitemapIndex()
{
var sitemapIndex = new SitemapIndex();
// basic usage:
sitemapIndex.Add(new SitemapIndexNode(Url.Action("Sitemap1")));
// or: this extension function fixes the null reference warning
// on the line above:
var addResult = sitemapIndex.TryAdd(Url.Action("Sitemap2"));
// or: use the Create function
sitemapIndex.Add(SitemapIndexNode.Create(Url.Action("Sitemap1")));
return new SitemapResult(sitemapIndex);
}
[Route("sitemap-1.xml")]
public IActionResult Sitemap1()
{
// ...
}
[Route("sitemap-2.xml")]
public IActionResult Sitemap2()
{
// ...
}
Advanced setup and extensions
See the Sidio.Sitemap.Core package documentation to read more about additional properties and sitemap extensions (i.e. news, images and videos).
Using middleware
By using the SitemapMiddlware the sitemap is generated automatically using reflection.
ASP .NET Core controllers and actions are supported, as well as Razor pages and API controllers.
Setup
In Program.cs, add the following:
// di setup
builder.Services.
.AddHttpContextAccessor()
.AddDefaultSitemapServices<HttpContextBaseUrlProvider>()
.AddSitemapMiddleware(
options =>
{
options.EndpointInclusionMode = EndpointInclusionMode.OptIn;
options.CacheEnabled = false; // (optional) default is false, set to true to enable caching
options.CacheAbsoluteExpirationInMinutes = 60; // (optional) default is 60 minutes
})
// use the middleware
app.UseSitemap();
Controller and action attributes
Decorate your controllers and/or actions with the [SitemapInclude] or [SitemapExclude] attribute.
When using OptIn mode, only controllers and/or actions decorated with [SitemapInclude] will be included in the sitemap.
[SitemapInclude] // this action will be included in the sitemap
public IActionResult Index()
{
return View();
}
When using OptOut mode, controllers and/or actions decorated with [SitemapExclude] will be excluded from the sitemap.
[SitemapExclude] // this action will not be included in the sitemap
public IActionResult Index()
{
return View();
}
API controllers
Indexing of API controllers is supported as well by configuring the SitemapMiddleware:
builder.Services
// ...
.AddSitemapMiddleware(
options =>
{
// ...
options.IncludeApiControllers = true;
})
Razor pages
Similar to controllers and actions, the attributes can be used in razor pages:
@page
@attribute [SitemapExclude]
@model LoginModel
@{
ViewData["Title"] = "My login page";
}
Caching
Configure the HybridCache to use caching of the Sitemap.
builder.Services.AddHybridCache();
builder.Services
// ...
.AddSitemapMiddleware(
options =>
{
// ...
options.CacheEnabled = true;
options.CacheDurationInMinutes = 60; // optional, default is 60 minutes
options.LocalCacheDurationInMinutes = 5; // optional, default is 5 minutes
})
Providing additional nodes
You can provide additional sitemap nodes by implementing the ISitemapNodeProvider interface. The middleware will
detect and use your implementation automatically.
// Implement the ICustomSitemapNodeProvider interface
public class MyCustomSitemapNodeProvider : ICustomSitemapNodeProvider
{
public IEnumerable<SitemapNode> GetNodes()
{
return new List<SitemapNode> { new("/test") };
}
}
// Register the provider in DI
services.AddCustomSitemapNodeProvider<MyCustomSitemapNodeProvider>();
Upgrade to v3.x
In version 3.x, the IDistributedCache is replaced with the HybridCache. Register the HybridCache in your startup file:
builder.Services.AddHybridCache();
Options
builder.Services.AddSitemapMiddleware(
options =>
{
- options.CacheAbsoluteExpirationInMinutes = 60;
+ options.CacheDurationInMinutes = 60;
})
FAQ
- Exception:
Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'Sidio.Sitemap.AspNetCore.HttpContextBaseUrlProvider'.- Solution: call
services.AddHttpContextAccessor();to register theIHttpContextAccessor.
- Solution: call
See also
- Sidio.Sitemap.Core package
- Sidio.Sitemap.Blazor package for Blazor support.
Used by
| Product | Versions 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.1.0)
- Sidio.Sitemap.Core (>= 2.8.0)
-
net8.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.1.0)
- Sidio.Sitemap.Core (>= 2.8.0)
-
net9.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.1.0)
- Sidio.Sitemap.Core (>= 2.8.0)
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 | |
|---|---|---|---|
| 3.1.0 | 1,528 | 1/7/2026 | |
| 3.0.3 | 2,135 | 11/12/2025 | |
| 3.0.2-preview | 890 | 9/18/2025 | |
| 3.0.1-preview | 766 | 6/10/2025 | |
| 3.0.0-preview | 429 | 3/20/2025 | |
| 2.8.0 | 1,068 | 11/12/2025 | |
| 2.7.7 | 631 | 10/24/2025 | |
| 2.7.6 | 3,701 | 5/20/2025 | |
| 2.7.5 | 2,191 | 4/15/2025 | |
| 2.7.4 | 1,042 | 3/18/2025 | |
| 2.7.3 | 531 | 2/24/2025 | |
| 2.7.2 | 1,333 | 1/22/2025 | |
| 2.7.1 | 2,125 | 1/2/2025 | |
| 2.6.0 | 3,197 | 11/13/2024 | |
| 2.5.4 | 724 | 11/7/2024 |