Sidio.Sitemap.Blazor is a lightweight .NET library for generating sitemaps in Blazor server applications.
Add the package to your project.
Register services:
builder.Services
.AddHttpContextAccessor()
.AddDefaultSitemapServices<HttpContextBaseUrlProvider>();
Register the middleware. Make sure to choose the correct namespace.
using Sidio.Sitemap.Blazor;
app.UseSitemap();
Add the following attribute to your components (pages) to include them in the sitemap:
@* default *@
@attribute [Sitemap]
@* override route url *@
@attribute [Sitemap("/custom-url")]
@* add change frequency, priority and last modified date *@
@attribute [Sitemap(ChangeFrequency.Daily, 0.5, "2024-01-01")]
The sitemap is accessible at [domain]/sitemap.xml
.
- 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