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:
- Install-Package AspNetCore.SEOHelper
Sitemaps Format
The sitemap.xml file must be encoded with UTF-8. Example of sitemap:
- <?xml version="1.0" encoding="utf-8"?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <url>
- <loc>https://www.codingwithesty.com/</loc>
- <lastmod>2020-05-07T13:32:30+06:00</lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- <url>
- <loc>https://www.codingwithesty.com/search-engine-optimization-library-for-dot-net-code-developers</loc>
- <lastmod>2020-05-07T13:32:30+06:00</lastmod>
- <changefreq>daily</changefreq>
- <priority>0.8</priority>
- </url>
- </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:
- always
- hourly
- daily
- weekly
- monthly
- yearly
- never
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
Nalin JayasuriyaPosted Mar 22, 2021, 9:49 AM
Thank you. I just want to say that in addition to this approach, another is to manually create a sitemap.xml and place the file in the ASP.NET application's wwwroot folder (make sure you have app.UseStaticFiles(); in the startup.cs ). Also do the same for the robots.txt file. You can then submit your sitemap.xml to a search engine (e.g. Google)
Gert VanNuffelPosted Mar 15, 2021, 3:56 PM
It's not working. When I go to https://localhost:{port}/sitemap.xml, the sitemap is empty. "This XML file does not appear to have any style information associated with it. The document tree is shown below."
Christian PonziPosted Dec 6, 2020, 4:07 PM
Hi i follow your istruction and i copy every single code line. the result is an empty xml. Did I forgot something?javascript:void(0)