Sidio.Sitemap.Blazor is a lightweight .NET library for generating sitemaps in Blazor server applications.
| 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 |
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.
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>();- 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
- Build error:
The call is ambiguous between the following methods or properties: 'Sidio.Sitemap.Blazor.ApplicationBuilderExtensions.UseSitemap(...)' and 'Sidio.Sitemap.AspNetCore.Middleware.ApplicationBuilderExtensions.UseSitemap(...)'- Solution: make sure to use the correct namespace:
using Sidio.Sitemap.Blazor;, and notusing Sidio.Sitemap.AspNetCore.Middleware;.
- Solution: make sure to use the correct namespace: