Skip to content

Sidio.Sitemap.AspNetCore is a lightweight .NET library for generating sitemaps and a sitemap index in ASP .NET Core applications.

License

Notifications You must be signed in to change notification settings

marthijn/Sidio.Sitemap.AspNetCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

build NuGet Version Coverage Status

Installation

Add the package 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

// 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);
}

Sitemap and sitemap index

[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 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:

// 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();

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.

[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();
}

Razor pages

Similar to controllers and actions, the attributes can be used in razor pages:

@page
@attribute [SitemapExclude]
@model LoginModel
@{
    ViewData["Title"] = "My login page";
}

Caching

Configure the IDistributedCache 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

Used by

About

Sidio.Sitemap.AspNetCore is a lightweight .NET library for generating sitemaps and a sitemap index in ASP .NET Core applications.

Topics

Resources

License

Security policy

Stars

Watchers

Forks