https://github.com/marthijn/sidio.sitemap.aspnetcore
Sidio.Sitemap.AspNetCore is a lightweight .NET library for generating sitemaps and a sitemap index in ASP .NET Core applications.
https://github.com/marthijn/sidio.sitemap.aspnetcore
asp-net-core generating-sitemaps sitemap sitemap-files sitemap-generator
Last synced: 9 months ago
JSON representation
Sidio.Sitemap.AspNetCore is a lightweight .NET library for generating sitemaps and a sitemap index in ASP .NET Core applications.
- Host: GitHub
- URL: https://github.com/marthijn/sidio.sitemap.aspnetcore
- Owner: marthijn
- License: mit
- Created: 2024-02-09T15:20:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-10-09T05:20:34.000Z (10 months ago)
- Last Synced: 2025-10-19T19:45:27.299Z (9 months ago)
- Topics: asp-net-core, generating-sitemaps, sitemap, sitemap-files, sitemap-generator
- Language: C#
- Homepage:
- Size: 1.18 MB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
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.
[](https://www.nuget.org/packages/Sidio.Sitemap.AspNetCore/)
# Versions
| | [Sidio.Sitemap.Core](https://github.com/marthijn/Sidio.Sitemap.Core)| [Sidio.Sitemap.AspNetCore](https://github.com/marthijn/Sidio.Sitemap.AspNetCore) | [Sidio.Sitemap.Blazor](https://github.com/marthijn/Sidio.Sitemap.Blazor) |
|------------|---------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| *NuGet* | [](https://www.nuget.org/packages/Sidio.Sitemap.Core/) | [](https://www.nuget.org/packages/Sidio.Sitemap.AspNetCore/) | [](https://www.nuget.org/packages/Sidio.Sitemap.Blazor/) |
| *Build* | [](https://github.com/marthijn/Sidio.Sitemap.Core/actions/workflows/build.yml)| [](https://github.com/marthijn/Sidio.Sitemap.AspNetCore/actions/workflows/build.yml) | [](https://github.com/marthijn/Sidio.Sitemap.Blazor/actions/workflows/build.yml) |
| *Coverage* | [](https://coveralls.io/github/marthijn/Sidio.Sitemap.Core?branch=main)| [](https://coveralls.io/github/marthijn/Sidio.Sitemap.AspNetCore?branch=main) | [](https://coveralls.io/github/marthijn/Sidio.Sitemap.Blazor?branch=main) |
| *Requirements*|.NET Standard, .NET 8+, | .NET 8+, AspNetCore|.NET 8+, AspNetCore, Blazor server|
# Installation
Add [the package](https://www.nuget.org/packages/Sidio.Sitemap.AspNetCore/) 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
```csharp
// di setup
services.AddHttpContextAccessor();
services.AddDefaultSitemapServices();
// controller
[HttpGet]
public IActionResult Sitemap()
{
var nodes = new List { new ("page.html"), SitemapNode.Create(Url.Action("Index")) };
var sitemap = new Sitemap(nodes);
return new SitemapResult(sitemap);
}
```
### Sitemap and sitemap index
```csharp
[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](https://github.com/marthijn/Sidio.Sitemap.Core) 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.
Currently only ASP .NET Core controllers and actions are supported. Razor pages will be supported in the future.
### Setup
In `Program.cs`, add the following:
```csharp
// di setup
builder.Services.
.AddHttpContextAccessor()
.AddDefaultSitemapServices()
.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.
```csharp
[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.
```csharp
[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`:
```csharp
builder.Services
// ...
.AddSitemapMiddleware(
options =>
{
// ...
options.IncludeApiControllers = true;
})
```
### Razor pages
Similar to controllers and actions, the attributes can be used in razor pages:
```cshtml
@page
@attribute [SitemapExclude]
@model LoginModel
@{
ViewData["Title"] = "My login page";
}
```
### Caching
Configure the [`IDistributedCache`](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed) to use caching of the Sitemap.
# 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 the `IHttpContextAccessor`.
# See also
* [Sidio.Sitemap.Core package](https://github.com/marthijn/Sidio.Sitemap.Core)
* [Sidio.Sitemap.Blazor package](https://github.com/marthijn/Sidio.Sitemap.Blazor) for Blazor support.
# Used by
- [Drammer.com](https://drammer.com)