Introduction

A sitemap is an XML file that contains all URLs of a website. When a website creates dynamic web pages, it is very difficult for Search Engines to search all URLs and index them. That is why we created a sitemap XML file and include all required URLs in it. It helps search engines find URLs easily and index them quickly. In this article, we will create a sitemap.xml file and add a middleware for sitemap routing.
The default URL is https://example.com/sitemap.xml

Required Packages

Let's create an ASP.NET Core Web Application and open the Package Manager Console and install the AspNetCore.SEOHelper NuGet package .
We can execute the following command in the Package Manager Console window:
  1. Install-Package AspNetCore.SEOHelper

Sitemaps Format

The sitemap.xml file must be encoded with UTF-8. Example of sitemap:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  3. <url>
  4. <loc>https://www.codingwithesty.com/</loc>
  5. <lastmod>2020-05-07T13:32:30+06:00</lastmod>
  6. <changefreq>daily</changefreq>
  7. <priority>1.0</priority>
  8. </url>
  9. <url>
  10. <loc>https://www.codingwithesty.com/search-engine-optimization-library-for-dot-net-code-developers</loc>
  11. <lastmod>2020-05-07T13:32:30+06:00</lastmod>
  12. <changefreq>daily</changefreq>
  13. <priority>0.8</priority>
  14. </url>
  15. </urlset>
urlset - Sitemap start and ending tag. Sitemap document start with <urlset> and ends with </urlset>
URL - parent tag for each URL entry
loc - URL of our site.
priority - represents site map priority.
lastmod - when the web page was last modified.
changefreq - This is an optional tag. It suggests how often a user agent should visit it again
The values are:

Create sitemap.xml

SEOHelper package provides SitemapNode class for new URL. CreateSitemapXML method helps to create sitemap.xml file. Let's create a list of URLs and then create sitemap.xml file in our project root directroy
  1. public class HomeController : Controller
  2. {
  3. private readonly ILogger<HomeController> _logger;
  4. private readonly IWebHostEnvironment _env;
  5. public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
  6. {
  7. _logger = logger;
  8. _env = env;
  9. }
  10. //not returning any view
  11. public string CreateSitemapInRootDirectory()
  12. {
  13. var list = new List<SitemapNode>();
  14. list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://www.example.com/asp-dot-net-turotial-part1", Frequency = SitemapFrequency.Daily });
  15. list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://www.example.com/asp-dot-net-turotial-part2", Frequency = SitemapFrequency.Yearly });
  16. list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.7, Url = "https://www.example.com/asp-dot-net-turotial-part3", Frequency = SitemapFrequency.Monthly });
  17. new SitemapDocument().CreateSitemapXML(list, _env.ContentRootPath);
  18. return "sitemap.xml file should be create in root directory";
  19. }
  20. }
CreateSitemapInRootDirectory() methid creates sitemap.xml in our project root directory.

Routing sitemap.xml in Asp.net Core

Now go to startup.cs and add app.UseXMLSitemap(env.ContentRootPath); in the middleware.
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. app.UseXMLSitemap(env.ContentRootPath);
  4. app.UseRouting();
  5. app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
  6. }
Now, run your project and navigate to https://localhost:port/sitemap.xml .

Conclusion

This is the end of our sitemap.xml article. Thanks a lot for reading. Write in the comment box in case you have any questions.