Card

Create a Sitemap in ASP.NET

A Sitemap is an XML file that shows your website URLs about your articles, blogs, movies, files and etc. When google crawler wants to craw your website, the sitemap gives updated information about your links to the crawler. Thus, it is very important that we create a sitemap in our project and manage it. In this article, I will show you how to create a sitemap and manage it.

  1. Create a new ASP.NET project
  2. Create a file in website root called sitmap.xml with following codes:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">


</urlset>

  1. Create a .cs class called updateSiteMap.cs with below codes:
public class updateSiteMap
    {
       public void UpdateSiteMap(string Addr, string NewOpr)
        {
            XmlDocument xmlDoc = new XmlDocument(); 
            xmlDoc.Load(Path.GetFullPath(@"./wwwroot/Sitemap.xml"));
            bool find = false;
            if (NewOpr != "add")
            {
                XmlElement xmlElement = xmlDoc.DocumentElement;
                if (xmlElement.ChildNodes != null)
                {
                    foreach (XmlElement myElement in xmlDoc.DocumentElement)
                    {
                       if (myElement.ChildNodes[0].InnerText == Addr)
                        {
                            find = true;
                            if (NewOpr != "delete")
                                myElement.ChildNodes[1].InnerText = DateTime.Now.ToString("yyyy-MM-dd");
                            else
                                myElement.ParentNode.RemoveChild(myElement);
                            break;
                        }
                    }

                    if (find == false)
                    {
                        string ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                        XmlNode url = xmlDoc.CreateNode(XmlNodeType.Element, "url", ns);
                        XmlNode loc = xmlDoc.CreateNode(XmlNodeType.Element, "loc", ns);
                        XmlNode lastmod = xmlDoc.CreateNode(XmlNodeType.Element, "lastmod", ns);
                        XmlNode changefreq = xmlDoc.CreateNode(XmlNodeType.Element, "changefreq", ns);
                        XmlNode priority = xmlDoc.CreateNode(XmlNodeType.Element, "priority", ns);
                        loc.InnerText = Addr;
                        lastmod.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
                        changefreq.InnerText = "always";
                        priority.InnerText = "1";
                        url.AppendChild(loc);
                        url.AppendChild(lastmod);
                        url.AppendChild(changefreq);
                        url.AppendChild(priority);
                        xmlDoc.DocumentElement.AppendChild(url);
                    }
                }
            }

            else

            {
                string ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XmlNode url = xmlDoc.CreateNode(XmlNodeType.Element, "url", ns);
                XmlNode loc = xmlDoc.CreateNode(XmlNodeType.Element, "loc", ns);
                XmlNode lastmod = xmlDoc.CreateNode(XmlNodeType.Element, "lastmod", ns);
                XmlNode changefreq = xmlDoc.CreateNode(XmlNodeType.Element, "changefreq", ns);
                XmlNode priority = xmlDoc.CreateNode(XmlNodeType.Element, "priority", ns);
                loc.InnerText = Addr;
                lastmod.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
                changefreq.InnerText = "always";
                priority.InnerText = "1";
                url.AppendChild(loc);
                url.AppendChild(lastmod);
                url.AppendChild(changefreq);
                url.AppendChild(priority);
                xmlDoc.DocumentElement.AppendChild(url);
            }
            xmlDoc.Save(Path.GetFullPath(@"./wwwroot/Sitemap.xml"));
        }
    }
  1. Whenever you want to update your sitemap, all you have to do is call the following function and pass the url:

           

     var baseLocation = new Uri($"{Request.Scheme}://{Request.Host}") +URL;
                updateSiteMap sitemap = new updateSiteMap();
                sitemap.UpdateSiteMap(baseLocation, "add);

in which "add" to create a new sitemap and "edit" for the update are using. For example:

var baseLocation = new Uri($"{Request.Scheme}://{Request.Host}") + "article? Id=12";
            updateSiteMap site = new updateSiteMap();
            site.UpdateSiteMap(baseLocation , "add");

 

 

 

Recent Comments