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!

2
  • you need to config siteemap in your Web.config file May 16, 2020 at 22:14
  • @mehdifarhadi ASP.NET Core does not have a Web.Config. Guessing your referring to this: learn.microsoft.com/en-us/dotnet/api/… When you add those lines to the project does it automatically generate sitemap.xml?
    – SetupG
    May 17, 2020 at 1:30

1 Answer 1

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
        };
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.