https://github.com/pscourtney/blazorserverdynamicsitemap
A dynamic sitemap xml generator for blazor server
https://github.com/pscourtney/blazorserverdynamicsitemap
blazor blazorserver sitemap
Last synced: 5 months ago
JSON representation
A dynamic sitemap xml generator for blazor server
- Host: GitHub
- URL: https://github.com/pscourtney/blazorserverdynamicsitemap
- Owner: PSCourtney
- License: mit
- Created: 2023-02-08T17:23:36.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-12T08:45:30.000Z (over 1 year ago)
- Last Synced: 2025-04-22T14:29:14.552Z (about 1 year ago)
- Topics: blazor, blazorserver, sitemap
- Language: C#
- Homepage:
- Size: 481 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dynamic Sitemap for Blazor Server
A dynamic sitemap xml generator for Blazor Server.

## Usage
Make sitemap.xml a navigible endpoint in `Program.cs`
```cs
app.UseEndpoints(endpoints =>
{
app.MapGet("/sitemap.xml", (SitemapXml sitemapXml) => sitemapXml.Generate());
});
```
Decide on how to build up your `Classes.Utils.Sitemap` class and call it in `sitemapXml.Generate()`. Below a `List` is used to fill the sitemap with dummy endpoints. This could be done statically like this or via a database call.
```cs
Sitemap = new Classes.Utils.Sitemap();
//Dummy data, like from a database. Dynamic pages
List classData = new List
{
new WebPage { Domain = "example1.com", Prefix = "https", Path = "/path1" },
new WebPage { Domain = "example2.com", Prefix = "https", Path = "/path2" },
new WebPage { Domain = "example3.com", Prefix = "https", Path = "/path3" }
};
foreach (var item in classData)
{
Sitemap.Add(new SitemapLocation
{
ChangeFrequency = SitemapLocation.eChangeFrequency.yearly,
Url = $"{item.Prefix}://{item.Domain}{item.Path}"
});
}
```
It is also possible to call a list of the websites page or component endpoints.
```cs
// Get all the components which are not dynamic who have a base class of ComponentBase
var components = Assembly.GetExecutingAssembly().ExportedTypes.Where(t => typeof(ComponentBase).IsAssignableFrom(t)).ToList();
var routables = components.Where(c => c.GetCustomAttributes(inherit: true).OfType().Any());
foreach (var routable in routables)
{
Sitemap.Add(new SitemapLocation
{
ChangeFrequency = SitemapLocation.eChangeFrequency.yearly,
Url = $"https://example3.com/{routable}"
});
}
```