【问题标题】:Serving dynamic XML file from .NET Core Web Application从 .NET Core Web 应用程序提供动态 XML 文件
【发布时间】:2019-10-24 17:40:40
【问题描述】:

在我的 .NET Core 2.2 网站中,我的 BlogController 中有一个控制器方法,它生成一个 sitemap.xml 文件:

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

我设置了这条路线,以便站点地图将在https://mysite/sitemap 输出

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

这行得通,因为访问 /sitemap 会导致 XML 内容被提供。

但是,当我访问 https://mysite/sitemap.xml 时,我收到 404 错误。

我很确定这与静态文件处理有关,但我不确定如何设置它以便/sitemap.xml 工作。

【问题讨论】:

  • 如果您想访问https://mysite/sitemap.xml,请确保sitemap.xml 文件存在于wwwroot 文件夹中。
  • return Content 只是生成一个xml文件显示在浏览器上,而不是上传到服务器。

标签: asp.net-core asp.net-mvc-routing asp.net-core-2.2


【解决方案1】:

您可以尝试像这样动态构建 xml

            [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();
            }

或创建一个类似https://forums.asp.net/t/2160949.aspx?Create+Robots+txt+and+sitemap+xml+dynamically+in+asp+net+core的页面

【讨论】:

    【解决方案2】:

    这是一个简单的演示,介绍如何使用https://mysite/sitemap.xml 之类的 url 生成 xml 文件到服务器并显示 xml 文件:

    1.查看:

    <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.控制器:

    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.一定要像下面这样添加app.UseStaticFiles();,然后你就可以访问https://mysite/sitemap.xml

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

    4.结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多