This is an update to a previous post showing how I generate a sitemap file for search engine indexing. This has been updated from the previous ASP.Net to ASP.NET Core using C# and expanded to include extra information.
Search engine optimisation (SEO) is crucial for the visibility and success of any website. One key aspect of SEO is ensuring that search engines can efficiently crawl and index your site. This is where a sitemap can improve your visibility with search engines.
A sitemap is an XML file that lists the URLs of your website, along with metadata such as the last modified date, update frequency, and priority. It helps search engines like Google discover and understand the structure of your website. In this blog post, I will show you how I generate a Google-compliant sitemap.xml file using ASP.NET Core and C#.
Why You Need a Sitemap
Before we go into the code, here are a few benefits of having a sitemap:
- Improved indexing: Helps search engines find and index all important pages, especially those that aren’t well linked internally.
- Metadata support: Allows you to specify additional data like update frequency and priority.
- Better crawl efficiency: Assists crawlers in navigating large or complex sites more effectively.
Step 1: Create the Sitemap Endpoint
Create a new controller in your project called SiteMapController.
Here's how to implement a sitemap using a controller to generate an XML sitemap:
using BlogRazor.Context; using Microsoft.AspNetCore.Mvc; using System.Globalization; using System.Text; using System.Xml; [ApiController] [Route("[controller]")] public class SitemapNewController : ControllerBase { [HttpGet("/sitemap.xml")] public IActionResult GetSitemap() { // Get the base url of your website var baseUrl = $"{Request.Scheme}://{Request.Host}"; using var dbContext = new BlogContext(); // Load pages to include in the sitemap from your database or model var pages = dbContext.Posts.OrderByDescending(x => x.DateCreated).ToList(); // Manually create the root and entries using XmlWriter var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, OmitXmlDeclaration = false }; using var stream = new MemoryStream(); using var writer = XmlWriter.Create(stream, settings); writer.WriteStartDocument(); writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9"); // First we add the website home page as a manual entry, you can repeat this // to add other static pages. If you have a last updated date you can set this here writer.WriteStartElement("url"); writer.WriteElementString("loc", baseUrl); writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz")); writer.WriteElementString("changefreq", "weekly"); writer.WriteElementString("priority", "0.5"); writer.WriteEndElement(); // // Add each page or post to the list foreach (var page in pages) { // format the date var date = DateTime.Parse(page.DateCreated.GetValueOrDefault().ToString(CultureInfo.CurrentCulture)).ToString("yyyy-MM-ddTHH:mm:sszzz"); writer.WriteStartElement("url"); // write the url, you can change this to match your website url structure writer.WriteElementString("loc", baseUrl + "/post/" + page.Slug); writer.WriteElementString("lastmod", date); writer.WriteElementString("changefreq", "weekly"); writer.WriteElementString("priority", "0.5"); writer.WriteEndElement(); // } writer.WriteEndElement(); // writer.WriteEndDocument(); writer.Flush(); stream.Position = 0; var xml = Encoding.UTF8.GetString(stream.ToArray()); return Content(xml, "application/xml", Encoding.UTF8); } }
Step 2: Run and Test
Run your application and visit:
https://localhost:5001/sitemap.xml
You’ll receive a fully valid sitemap.xml output, compliant with Google’s standards.
Step 3: Submit to Google
Once you’ve deployed your site, go to Google Search Console and submit your sitemap URL (usually /sitemap.xml).
Comments