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:
- <lastmod>2020-05-07T13:32:30+06:00</lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- <url>
- <loc>https:
- <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
- public class HomeController : Controller
- {
- private readonly ILogger<HomeController> _logger;
- private readonly IWebHostEnvironment _env;
- public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
- {
- _logger = logger;
- _env = env;
- }
-
-
- public string CreateSitemapInRootDirectory()
- {
- var list = new List<SitemapNode>();
- list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://www.example.com/asp-dot-net-turotial-part1", Frequency = SitemapFrequency.Daily });
- list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://www.example.com/asp-dot-net-turotial-part2", Frequency = SitemapFrequency.Yearly });
- list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.7, Url = "https://www.example.com/asp-dot-net-turotial-part3", Frequency = SitemapFrequency.Monthly });
-
- new SitemapDocument().CreateSitemapXML(list, _env.ContentRootPath);
- return "sitemap.xml file should be create in root directory";
- }
-
- }
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.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- app.UseXMLSitemap(env.ContentRootPath);
- app.UseRouting();
- app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
- }
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.