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.
// di setup
services.AddHttpContextAccessor();
services.AddDefaultSitemapServices<HttpContextBaseUrlProvider>();
// controller
[HttpGet]
public IActionResult Sitemap()
{
var nodes = new List<SitemapNode> { new ("page.html"), new (Url.Action("Index")) };
var sitemap = new Sitemap(nodes);
return new SitemapResult(sitemap);
}
[Route("sitemap.xml")]
public IActionResult SitemapIndex()
{
var sitemapIndex = new SitemapIndex();
sitemapIndex.Add(new SitemapIndexNode(Url.Action("Sitemap1")));
return new SitemapResult(sitemapIndex);
}
[Route("sitemap-1.xml")]
public IActionResult Sitemap1()
{
// ...
}
- 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