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.
Add the package to your project.
There are two ways to generate sitemaps: manually or by using middleware. When using middleware, the sitemap is generated automatically.
// 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);
}
[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()
{
// ...
}
See the Sidio.Sitemap.Core package documentation to read more about additional properties and sitemap extensions (i.e. news, images and videos).
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.
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();
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();
}
Similar to controllers and actions, the attributes can be used in razor pages:
@page
@attribute [SitemapExclude]
@model LoginModel
@{
ViewData["Title"] = "My login page";
}
Configure the IDistributedCache
to use caching of the Sitemap.
- 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
- Sidio.Sitemap.Core package
- Sidio.Sitemap.Blazor package for Blazor support.