https://github.com/hallojoe/xml-sitemaps
A package for creating XML sitemaps in AspNetCore applications.
https://github.com/hallojoe/xml-sitemaps
sitemap xml
Last synced: 3 months ago
JSON representation
A package for creating XML sitemaps in AspNetCore applications.
- Host: GitHub
- URL: https://github.com/hallojoe/xml-sitemaps
- Owner: hallojoe
- License: mit
- Created: 2024-08-21T18:44:13.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-22T10:28:08.000Z (almost 2 years ago)
- Last Synced: 2025-08-22T16:24:56.412Z (10 months ago)
- Topics: sitemap, xml
- Language: C#
- Homepage: https://github.com/hallojoe/xml-sitemaps
- Size: 832 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XML Sitemaps
Zero config XML sitemaps. A thing for creating XML sitemaps in AspNetCore applications.
## Get started
Follow these instructions to get started.
### Install
`dotnet add package Casko.AspNetCore.XmlSiteMaps --version 1.0.0`
### Configure
In `Startup.cs` or `Program.cs`, add XML sitemaps on `IServiceCollection`:
```
services.AddXmlSiteMaps();
```
In `Startup.cs` or `Program.cs`, use XML sitemaps on `IApplicationBuilder`:
```
app.UseXmlSiteMaps(addRewrites: true); // true is default
```
If ordering creates problems for rewrites then set `useRewrites: false` and then call where it fit in:
```
app.UseXmlSiteMapsRewrites();
```
After configuring XmlSiteMaps, then implementations of `IXmlSiteMap`, `IXmlSiteMap`, `IXmlSiteMapCollection` and `IXmlSiteMapCollection` will be picked up on application startup and endpoints and rewrites will be setup.
XML sitemaps will be served from `api/xml-sitemaps/...` and URL rewrites will be created and point `FileName` to it's corresponding endpoint.
### Implement
Example code for creating different XML sitemap things.
#### Create a single XML sitemap:
Simplest for of XML sitemap.
```
public class XmlSiteMapService : IXmlSiteMap
{
public string FileName => "sitemap.xml";
public XmlSiteMap GetXmlSiteMap()
{
var xmlSiteMap = new XmlSiteMap();
xmlSiteMap.Urls.Add(new XmlSiteMapUrl()
{
Location = "https://...",
LastModified = DateTime.UtcNow,
ChangeFrequency = ChangeFrequency.Daily,
Priority = .5
});
...
return xmlSiteMap;
}
}
```
#### Create a XML sitemap index:
When multiple XML sitemaps exist you will need at least one XML sitemap index.
```
public class XmlSiteMapIndexService : IXmlSiteMap
{
public string FileName => "sitemap.xml";
public XmlSiteMapIndex GetXmlSiteMap()
{
var xmlSiteMapIndex = new XmlSiteMapIndex();
xmlSiteMap.Locations.Add(new XmlSiteMapIndexLocation()
{
Location = "https://...",
});
...
return xmlSiteMapIndex;
}
}
```
#### Create XML sitemap collection:
Create many XML sitemaps with a single implementation.
```
public class XmlSiteMapCollectionService() : IXmlSiteMapCollection
{
public IDictionary Routes => new Dictionary()
{
{ "en", "sitemap-collection-default.xml" },
{ "da", "sitemap-collection-da.xml" },
{ "es", "sitemap-collection-es.xml" },
};
public XmlSiteMap GetXmlSiteMap(string key)
{
var xmlSiteMap = new XmlSiteMap();
...Create things using "key"
xmlSiteMap.Urls.Add(new XmlSiteMapUrl()
{
Location = $"https://...{key}",
LastModified = DateTime.UtcNow,
ChangeFrequency = ChangeFrequency.Daily,
Priority = .5
});
return xmlSiteMap;
}
}
```
## Changelog
### 2.0.0
- Add HttpContext to signature of `ISiteMap.GetXmlSiteMap`
- Pass HttpContext when building endpoints
### 1.0.1
- Remove required operator from Rel property of XHtmlLink
### 1.0.0
- Initialize project
## TODO
- Offer automatic XML sitemap index creation when creating `IXmlSitemapCollection`
- Make XHtmlLink not required