Configure sitemap in dotnet core

Author : Sachin Sharma
Published On : 12 Dec 2022

A sitemap is an XML file that contains all URLs of a website.When a website created dynamic pages,It's very dificult for google to crawl and index them.That's why we need sitemap XML file
and inculde all the pages url in it.Its help to index them.

In this article we will create a sitemap.xml file. there is many way to do that but we
will discuss over 2 ways.

1)create sitemap.xml by system.xml
2)create sitemap.xml by AspNetCore.SEOHelper 

1) Create sitemap.xml by system.xml

Before we create an XML, let's have a quick look at a simple XML file that we will create in this article.

 

sitemap/

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

[HttpGet, Route("sitemap.xml")]
        public async Task BlogSiteMap()
        {
            var syncIOFeature = HttpContext.Features.Get<IHttpBodyControlFeature>();
            syncIOFeature.AllowSynchronousIO = true;

            var data = await _blogService.GetAllPublishedBlog();

            Response.ContentType = "application/xml";

            using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings { Indent = true }))
            {
                xml.WriteStartDocument();
                xml.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

                foreach (var item in data)
                {
                    xml.WriteStartElement("url");
                    xml.WriteElementString("loc", string.Concat(AppSettingConfig.WebsiteUrl, "/article/",item.Slug));
                    xml.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                    xml.WriteElementString("changefreq", "daily");
                    xml.WriteElementString("priority", "1.0");

                    xml.WriteEndElement();
                }
                xml.WriteEndElement();
            }
        }


2)Create sitemap.xml by AspNetCore.SEOHelper :

We can create xml with nuget package AspNetCore.SEOHelper.this will create sitemap.xml 
file in root folder.

 
public string BlogSiteMap()  
        {  
            var list = new List<SitemapNode>();  
            foreach (var item in data)
            {
             list.Add(new SitemapNode { LastModified = DateTime.Now.ToString("yyyy-MM-dd"), Priority = 1.0, Url = "https://www.example.com/part1", Frequency = SitemapFrequency.Daily });  
            list.Add(new SitemapNode { LastModified = DateTime.Now.ToString("yyyy-MM-dd"), Priority = 1.0, Url = "https://www.example.com/part2", Frequency = SitemapFrequency.Daily });  
            list.Add(new SitemapNode { LastModified = DateTime.Now.ToString("yyyy-MM-dd"), Priority = 1.0, Url = "https://www.example.com/part3", Frequency = SitemapFrequency.Daily });  
             
            }
           
            new SitemapDocument().CreateSitemapXML(list, _env.ContentRootPath);  
            return "sitemap.xml";  
        } 

Routing sitemap.xml in Asp.net Core

 
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 http://localhost:port/sitemap.xml.

Comments

No comments have been added to this article.

Add Comment