【问题标题】:ASP.NET 3.1 SiteMap for GoogleASP.NET 3.1 谷歌站点地图
【发布时间】:2020-09-02 18:05:35
【问题描述】:

为带有 Razor 页面的 ASP.NET 3.1 Core 创建站点地图的正确方法是什么?另外值得注意的是,我在很多页面上都使用了参数,有没有办法通过某些类将这些参数添加到站点地图中?

我尝试过创建站点地图剃须刀页面并使用这样的在线生成器动态创建站点地图;

<?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>

但是,当我尝试导航到 /sitemap.xml 甚至 /sitemap 时,里面什么都没有。

还尝试只制作一个 .xml 文件,但是当我进行 Web 部署时,该文件仍然不存在。根据我的研究,听起来我需要在 startup.cs 中添加一些内容,但这似乎也没有什么不同。

感谢任何想法。谢谢!

【问题讨论】:

  • 您需要在您的 Web.config 文件中配置站点地图
  • @mehdifarhadi ASP.NET Core 没有 Web.Config。猜你指的是这个:docs.microsoft.com/en-us/dotnet/api/… 当你将这些行添加到项目中时,它会自动生成 sitemap.xml 吗?

标签: c# asp.net asp.net-core razor xml-sitemap


【解决方案1】:

在页面加载的剃须刀页面中,即 OnGet(),您可以将 ContentResult 作为 xml 返回。

伪代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2012-06-07
    • 2012-07-31
    相关资源
    最近更新 更多