<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Esty (c)</title>
    <description>The latest articles on DEV Community by Esty (c) (@estyc).</description>
    <link>https://dev.to/estyc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F385426%2F02ab029d-a627-4eb9-bef9-4f00947ce29b.png</url>
      <title>DEV Community: Esty (c)</title>
      <link>https://dev.to/estyc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/estyc"/>
    <language>en</language>
    <item>
      <title>Serving sitemap.xml and robots.txt with ASP.Net core MVC</title>
      <dc:creator>Esty (c)</dc:creator>
      <pubDate>Thu, 14 May 2020 08:36:41 +0000</pubDate>
      <link>https://dev.to/estyc/serving-sitemap-xml-and-robots-txt-with-asp-net-core-mvc-10f1</link>
      <guid>https://dev.to/estyc/serving-sitemap-xml-and-robots-txt-with-asp-net-core-mvc-10f1</guid>
      <description>&lt;p&gt;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 &lt;a href="https://www.example.com/robots.txt"&gt;https://www.example.com/robots.txt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some example of robots.txt:&lt;/p&gt;

&lt;p&gt;Blocking all content from all Search Engines: &lt;br&gt;
User-agent: *&lt;br&gt;
Disallow: /&lt;/p&gt;

&lt;p&gt;Allow all content for all Search Engines: &lt;br&gt;
User-agent: *&lt;br&gt;
Allow: /&lt;/p&gt;

&lt;p&gt;Sitemap.xml is an XML file that contains all URLs of a website that you want Search Engines to index. Sitemap XML file’s helps Search Engines find and index site quickly. The sitemap.xml file default location is: &lt;a href="https://www.example.com/sitemap.xml"&gt;https://www.example.com/sitemap.xml&lt;/a&gt;&lt;br&gt;
Example of sitemap file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;    
&amp;lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&amp;gt;    
  &amp;lt;url&amp;gt;    
    &amp;lt;loc&amp;gt;https://www.codingwithesty.com/&amp;lt;/loc&amp;gt;    
    &amp;lt;lastmod&amp;gt;2020-05-07T13:32:30+06:00&amp;lt;/lastmod&amp;gt;    
    &amp;lt;changefreq&amp;gt;daily&amp;lt;/changefreq&amp;gt;    
    &amp;lt;priority&amp;gt;1.0&amp;lt;/priority&amp;gt;    
  &amp;lt;/url&amp;gt;    
  &amp;lt;url&amp;gt;    
    &amp;lt;loc&amp;gt;https://www.codingwithesty.com/search-engine-optimization-library-for-dot-net-code-developers&amp;lt;/loc&amp;gt;    
    &amp;lt;lastmod&amp;gt;2020-05-07T13:32:30+06:00&amp;lt;/lastmod&amp;gt;    
    &amp;lt;changefreq&amp;gt;daily&amp;lt;/changefreq&amp;gt;    
    &amp;lt;priority&amp;gt;0.8&amp;lt;/priority&amp;gt;    
  &amp;lt;/url&amp;gt;    
&amp;lt;/urlset&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this article we will learn how to create simtemal.xml file and routing simtemal.xml in asp.net core. We also create routing for robots.txt file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Package Installation
&lt;/h1&gt;

&lt;p&gt;Create a new Asp.Net core project. Go to Nuget Package Manger install  &lt;a href="https://www.nuget.org/packages/AspNetCore.SEOHelper/"&gt;AspNetCore.SEOHelper&lt;/a&gt; package&lt;/p&gt;

&lt;h1&gt;
  
  
  Create sitemap.xml file:
&lt;/h1&gt;

&lt;p&gt;AspNetCore.SEOHelper  provides SitemapNode class for each URL and CreateSitemapXML&lt;br&gt;
() helps to create sitemap.xml file. Copy to following code in your controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class HomeController : Controller
    {
        private readonly ILogger&amp;lt;HomeController&amp;gt; _logger;
        private readonly IWebHostEnvironment _env;

        public HomeController(ILogger&amp;lt;HomeController&amp;gt; logger, IWebHostEnvironment env)
        {
            _logger = logger;
            _env = env;
        }


        public string Index()
        {
            var list = new List&amp;lt;SitemapNode&amp;gt;();

            list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://codingwithesty.com/serilog-mongodb-in-asp-net-core", Frequency = SitemapFrequency.Daily });
            list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.8, Url = "https://codingwithesty.com/logging-in-asp-net-core", Frequency = SitemapFrequency.Yearly });
            list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.7, Url = "https://codingwithesty.com/robots-txt-in-asp-net-core", Frequency = SitemapFrequency.Monthly });
            list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.5, Url = "https://codingwithesty.com/versioning-asp.net-core-apiIs-with-swagger", Frequency = SitemapFrequency.Weekly });
            list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.4, Url = "https://codingwithesty.com/configuring-swagger-asp-net-core-web-api", Frequency = SitemapFrequency.Never });

            new SitemapDocument().CreateSitemapXML(list, _env.ContentRootPath);
            return View();
        }

    }

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;CreateSitemapXML Method takes two parameters, the first parameter is SitemapNode list and the second parameter is the directory path where the sitemap.xml file will be stored. In this case we provide root directory of our project.&lt;/p&gt;

&lt;h1&gt;
  
  
  Routing sitemap.xml
&lt;/h1&gt;

&lt;p&gt;We have already created sitemap.xml file in our asp.net core project. Now we want to route this sitemap file like: &lt;a href="https://example.com/sitemap.xml"&gt;https://example.com/sitemap.xml&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add app.UseXMLSitemap middleware to the Configure method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseStaticFiles();

            app.UseXMLSitemap(env.ContentRootPath);


           app.UseRouting();
            app.UseEndpoints(endpoints =&amp;gt; { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
        }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now run your application and navigate to &lt;a href="https://hostname:port/sitemap.xml"&gt;https://hostname:port/sitemap.xml&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Routing robots.txt
&lt;/h1&gt;

&lt;p&gt;Routing robots.txt in similar to previous one. Just add this app.UseRobotsTxt middleware to your configure method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseStaticFiles();
            app.UseRobotsTxt(env.ContentRootPath);

            app.UseRouting();
            app.UseEndpoints(endpoints =&amp;gt; { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
        }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;UseRobotsTxt takes a directory path parameter. If there is no robots.txt file in the directory, it will show default text disallow all&lt;/p&gt;

&lt;p&gt;Now run your application and navigate to &lt;a href="https://hostname:port/robots.txt"&gt;https://hostname:port/robots.txt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks a lot for reading. Write in the comment box in case you have any questions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reference
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.codingwithesty.com/search-engine-optimization-library-for-dot-net-code-developers"&gt;Codingwithesty.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/esty-c/AspNetCore.SEOHelper/blob/master/README.md"&gt;AspNetCore.SEOHelper/github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sitemapxml</category>
      <category>robotstxt</category>
      <category>aspnetcore</category>
    </item>
  </channel>
</rss>
