112 lines
4.9 KiB
C#
112 lines
4.9 KiB
C#
using System.Text;
|
|
using System.Xml;
|
|
|
|
using CatherineLynwood.Models;
|
|
using CatherineLynwood.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace CatherineLynwood.Controllers
|
|
{
|
|
public class SitemapController : Controller
|
|
{
|
|
private DataAccess _dataAccess;
|
|
|
|
public SitemapController(DataAccess dataAccess)
|
|
{
|
|
_dataAccess = dataAccess;
|
|
}
|
|
|
|
// Action to generate the sitemap dynamically
|
|
[HttpGet("sitemap.xml")]
|
|
public async Task<IActionResult> Sitemap()
|
|
{
|
|
var urls = await GetUrlsForSitemap();
|
|
var sitemap = GenerateSitemapXml(urls);
|
|
|
|
return Content(sitemap, "application/xml", Encoding.UTF8);
|
|
}
|
|
|
|
// Method to retrieve all URLs for the sitemap
|
|
private async Task<List<SitemapEntry>> GetUrlsForSitemap()
|
|
{
|
|
var urls = new List<SitemapEntry>
|
|
{
|
|
new SitemapEntry { Url = Url.Action("Index", "Home", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("ContactCatherine", "Home", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("AboutCatherineLynwood", "Home", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("SamanthaLynwood", "Home", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("Index", "AskAQuestion", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("Index", "TheAlphaFlame", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("Blog", "TheAlphaFlame", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("Characters", "TheAlphaFlame", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
new SitemapEntry { Url = Url.Action("Discovery", "TheAlphaFlame", null, Request.Scheme).TrimEnd('/'), LastModified = DateTime.UtcNow },
|
|
// Additional static pages
|
|
};
|
|
|
|
string[] characters = { "Maggie", "Beth", "Rosie", "Rob", "Zoe", "Rick", "Rebecca", "Sophie" };
|
|
|
|
foreach(var character in characters)
|
|
{
|
|
urls.Add(new SitemapEntry
|
|
{
|
|
Url = Url.Action(character, "TheAlphaFlame", null, Request.Scheme).TrimEnd('/'),
|
|
LastModified = DateTime.UtcNow
|
|
});
|
|
}
|
|
|
|
// Add blog URLs dynamically, with PublishDate as LastModified
|
|
BlogIndex blogIndex = await _dataAccess.GetBlogsAsync(null);
|
|
foreach (var post in blogIndex.Blogs)
|
|
{
|
|
urls.Add(new SitemapEntry
|
|
{
|
|
Url = Url.Action("BlogItem", "TheAlphaFlame", new { slug = post.BlogUrl }, Request.Scheme).TrimEnd('/'),
|
|
LastModified = post.PublishDate
|
|
});
|
|
}
|
|
|
|
return urls;
|
|
}
|
|
|
|
// Generate XML sitemap from URL list
|
|
private string GenerateSitemapXml(List<SitemapEntry> entries)
|
|
{
|
|
var xmlSettings = new XmlWriterSettings
|
|
{
|
|
Indent = true,
|
|
Encoding = Encoding.UTF8,
|
|
OmitXmlDeclaration = false // Ensures the XML declaration is included
|
|
};
|
|
|
|
using var memoryStream = new MemoryStream();
|
|
using (var xmlWriter = XmlWriter.Create(memoryStream, xmlSettings))
|
|
{
|
|
xmlWriter.WriteStartDocument(); // Writes <?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
// Write <urlset> with both sitemap and xhtml namespaces
|
|
xmlWriter.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
|
|
xmlWriter.WriteAttributeString("xmlns", "xhtml", null, "http://www.w3.org/1999/xhtml");
|
|
|
|
foreach (var entry in entries)
|
|
{
|
|
xmlWriter.WriteStartElement("url");
|
|
xmlWriter.WriteElementString("loc", entry.Url);
|
|
xmlWriter.WriteElementString("lastmod", entry.LastModified.ToString("yyyy-MM-dd"));
|
|
xmlWriter.WriteElementString("changefreq", "weekly");
|
|
xmlWriter.WriteElementString("priority", "0.5");
|
|
xmlWriter.WriteEndElement();
|
|
}
|
|
|
|
xmlWriter.WriteEndElement(); // Closes <urlset>
|
|
xmlWriter.WriteEndDocument(); // Closes document
|
|
xmlWriter.Flush(); // Explicit flush to ensure all data is written
|
|
}
|
|
|
|
// Convert the memory stream to a UTF-8 encoded string
|
|
return Encoding.UTF8.GetString(memoryStream.ToArray());
|
|
}
|
|
|
|
}
|
|
}
|