find and solve ||
Please wait.....

Dynamic Sitemap.xml in ASP.NET Core

Dynamic Sitemap.xml in ASP.NET Core

Download Source Code

IN ASP.NET Core Generator Sitemap.xml Automatic and robots.txt for ASP.NET Core application.In your website you need to submit the Sitemap of that website website links/Url to Google Webmasters so that Google boost can crawl it and other internet users can find using Google search engine.

When you create dynamic web pages in your website, it is very difficult for Search Engines to search all URLs and index them.So you should creat a sitemap XML file and include all required URLs in it.So that search engines find URLs easily and index them quickly.

Create New Application in visual studio 2019

  1. Open your visual studio 
  2. Click on Create a New Project
  3. Select .NET Core Web Application and then Next
  4. Provide a Project name and confirm or change the Location. Select Create
  5. Select the latest version of ASP.NET Core in the drop-down (.NET Core 5.0)and then select Web Application
  6. Under Authentication, select Change and set the authentication to Individual User Accounts and then Select OK

In your website robots.txt is a text file which is used to tell Search Engines which URLs are allowed or disallowed for them to index.The file must be located at

Some example of robots.txt like as give below.

Allow all content

User-agent: *



Blocking all content

User-agent: *

Disallow: /.


Sitemap.xml is an XML file that contains all URLs links of a website that you we Search Engines to index.sitemap.xml is help to  Search Engines find and index site quickly which is default location like as

Create sitemap.xml file using C#

Create new SitemapViewModel.cs class under your Models folder and past following code.

    public class SitemapViewModel
        public string Loc { get; set; }
        public string Priority { get; set; }
        public string ChangeFreq { get; set; }
        public string LastModified { get; set;}

You can map database value in this SitemapViewModel.cs class but I have mapped static values in HomeController.cs. like as given below

Open your HomeController.cs and past following code.

        public async void SitemapXml()
           var data = new List<SitemapViewModel> {
            new SitemapViewModel { Loc="this-is-my-url-one",Priority="0.6",ChangeFreq="Daily",LastModified ="2019/02/01"},
            new SitemapViewModel { Loc="this-is-my-url-tow",Priority="0.8",ChangeFreq="Yearly",LastModified ="2020/03/04"},
            new SitemapViewModel { Loc="this-is-my-url-three",Priority="0.5",ChangeFreq="Daily",LastModified ="2021/05/02"},
            new SitemapViewModel { Loc="this-is-my-url-four",Priority="0.9",ChangeFreq="Weekly ",LastModified ="2021/06/07"},
            new SitemapViewModel { Loc="this-is-my-url-five",Priority="0.4",ChangeFreq="monthly",LastModified ="2021/02/08"},
            new SitemapViewModel { Loc="this-is-my-url-six",Priority="0.8",ChangeFreq="Daily",LastModified ="2021/09/03"},
            new SitemapViewModel { Loc="this-is-my-url-seven",Priority="0.9",ChangeFreq="Weekly ",LastModified ="2021/04/05"},
            string host = Request.Scheme + "://" + Request.Host;
            Response.ContentType = "application/xml";
            using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings { Indent = true }))
                xml.WriteStartElement("urlset", "");
                foreach (var item in data)
                    xml.WriteElementString("loc", host + item.Loc);

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 

Some changefreq  values are given below.

  1. always
  2. hourly
  3. daily
  4. weekly
  5. monthly
  6. yearly
  7. never

Put this in your Startup.cs class for either Kestrel or IIS:

If you wan to use in IIS then you can do that

   services.Configure<IISServerOptions>(options =>
        options.AllowSynchronousIO = true;

If use Kestrel, you can do that

    services.Configure<KestrelServerOptions>(options =>
        options.AllowSynchronousIO = true;

Now, run your project and navigate to https://localhost:port/sitemap.xml .


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

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

If felt valuable to you, feel free to share it.


Report Response