用下面的代码实现了
public static class SitemapEndpoint
{
public const string MapPath = "/sitemap";
public const string ContentType = "text/xml; charset=utf-8";
public static void MapSitemapEndpoint(this IEndpointRouteBuilder app)
{
app.MapGet(MapPath, ProduceSitemapAsync);
}
private static async Task ProduceSitemapAsync(HttpContext context, IBlogQueryService blogQueryService)
{
context.Response.ContentType = ContentType;
await using var xml = XmlWriter.Create(context.Response.Body, new XmlWriterSettings { Async = true, Indent = true });
await xml.WriteStartDocumentAsync();
await xml.WriteStartElementAsync(null, "urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
await xml.WriteEndElementAsync();
await xml.WriteEndDocumentAsync();
}
}