1

In my .NET Core 2.2 website, I have a controller method within my BlogController that generates a sitemap.xml file:

public ActionResult SiteMap()
{
     // logic here
     return Content("<sitemap>...</sitemap>", "text/xml");
}

I have this route set up so that the sitemap will be output at https://mysite/sitemap

routes.MapRoute(
                    name: "sitemap",
                    template: "sitemap",
                    defaults: new { controller = "Blog", action = "SiteMap" });

That works, in that accessing /sitemap results in the XML content being served up.

However, when I access https://mysite/sitemap.xml, I get a 404 error.

I'm pretty sure this is something to do with static file handling, but I'm not sure how to set it up so that /sitemap.xml works.

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • 1
    If you want to access `https://mysite/sitemap.xml`,be sure that `sitemap.xml` file exsits in wwwroot folder. – Rena Oct 25 '19 at 01:53
  • And `return Content` just generate a xml file to show on browser,not uploading to server. – Rena Oct 25 '19 at 02:00

2 Answers2

1

You could try to dynamtically build xml like this

            [Route("/sitemap.xml")]
            public void SitemapXml()
            {
                string host = Request.Scheme + "://" + Request.Host;
    
                Response.ContentType = "application/xml";
    
                using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings { Indent = true }))
                {
                    xml.WriteStartDocument();
                    xml.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
    
                    xml.WriteStartElement("url");
                    xml.WriteElementString("loc", host);
                    xml.WriteElementString("changefreq", "daily");
                    xml.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                    xml.WriteEndElement();
    
                    var categories = _categoryService.GetAllCategories(inclTopMenu: true);
                    foreach (var c in categories)
                        BuildXml(xml, c.GetSeName(), host);
    
    
    
                    xml.WriteEndElement();
                }
            }
            private void BuildXml(XmlWriter xml, string url, string host)
            {
                xml.WriteStartElement("url");
                xml.WriteElementString("loc", host + "/" + url);
                xml.WriteElementString("changefreq", "weekly");
                xml.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
                xml.WriteEndElement();
            }

or create a page like https://forums.asp.net/t/2160949.aspx?Create+Robots+txt+and+sitemap+xml+dynamically+in+asp+net+core

nam vo
  • 3,271
  • 12
  • 49
  • 76
0

Here is a simple demo about how to generate xml file to server and show xml file by using url like https://mysite/sitemap.xml:

1.View:

<form asp-action="Create" enctype="multipart/form-data">
<div class="form-group">
    <input  type="file" name="file" id="file" class="form-control" />
</div>
<div class="form-group">
    <input type="submit" value="Create" class="btn btn-default" />
</div>

2.Controller:

public class UsersController : Controller
{
    private IHostingEnvironment _env;
    public UsersController(IHostingEnvironment env)
    {
        _env = env;
    }
    [HttpGet]
    public IActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public async Task<IActionResult> Create(IFormFile file)
    {
        var fileName = System.IO.Path.GetFileName(file.FileName);
        var filePath = System.IO.Path.Combine(_env.WebRootPath, fileName);
        if (file.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
        }
        return View("Index");
    }
}

3.Be sure to add app.UseStaticFiles(); like below,then you could access https://mysite/sitemap.xml:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //...
    app.UseStaticFiles();      
    //...
}

4.Result: enter image description here

Rena
  • 30,832
  • 6
  • 37
  • 72