1

What is the proper way to create an sitemap for an ASP.NET 3.1 Core with Razor Pages? Also is worth noting I use parameters on a lot of pages is there a way to add those to the sitemap with some class?

I have tried creating a sitemap razor page and dynamically creating the sitemap using an online gernator like this;

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  <url>
    <loc>#mysiteURL</loc>
    <lastmod>2020-04-04T17:28:47+00:00</lastmod>
    <priority>1.00</priority>
  </url>
</urlset>

However when I try and navigate to say /sitemap.xml or even /sitemap nothing is in there.

Also tried making just an .xml file but when I do a web deploy this file still does not exist. From my research it sounds like I need to add something in startup.cs but that doesn't seem to make a difference either.

Any ideas is appreciated. Thanks!

John Conde
  • 217,595
  • 99
  • 455
  • 496
SetupG
  • 155
  • 2
  • 11
  • you need to config siteemap in your Web.config file – mehdi farhadi May 16 '20 at 22:14
  • @mehdifarhadi ASP.NET Core does not have a Web.Config. Guessing your referring to this: https://learn.microsoft.com/en-us/dotnet/api/system.web.sitemap?view=netframework-4.8 When you add those lines to the project does it automatically generate sitemap.xml? – SetupG May 17 '20 at 01:30

1 Answers1

0

In razor page on page load i.e OnGet() you can return ContentResult as xml.

Pseudo code

public IActionResult OnGet()
    {
         
     StringBuilder sb = new StringBuilder();
    // method which returns sitemap formatted xml string from db
     sb.Append(XmlGeneratedStringFromDataBase()); 

        return new ContentResult
        {
            ContentType = "application/xml",
            Content = sb.ToString(),
            StatusCode = 200
        };
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102