User-821857111 posted
Normally, you create a view or page that generates XML for the site map from a data source. The data source can be files on disk, content in a database etc. It depends on your project. You wouldn't actually start with an XML file. You could do that
same thing with robots.txt - mapping the URL to a Razor Page, for example, that is designed to output text/plain. Here's an example Razor Page that could work as your sitemap:
@page
@model SitemapModel
@{
Layout = null;
}
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach (var item in Model.Items)
{
<url>
<loc>@item.Url</loc>
<lastmod>@item.LastModified.ToW3CDate()</lastmod>
<changefreq>monthly</changefreq>
</url>
}
</urlset>
Model.Items
is a collection of data that you want to display. The ToW3CDate method is an extension method that outputs a DateTime to a format that is valid for the Sitemap protocol:
public static class DateTimeExtensions
{
public static string ToW3CDate(this DateTime dt)
{
return $"{dt.ToUniversalTime().ToString("s")}Z";
}
}