【问题标题】:ASP.Net C# MVC5 Razor View XML sitemapASP.Net C# MVC5 Razor 查看 XML 站点地图
【发布时间】:2015-01-10 16:00:32
【问题描述】:

我的控制器动作看起来像这样。

public ActionResult Sitemap()
        {
            Response.ContentType = "application/xml";
            return View();
        }

我的视图 Sitemap.cshtml 看起来像这样。

@{Layout = null;}<?xml version='1.0' encoding='UTF-8' ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
    <url>
        <loc>https://www.mywebsitename.com/home</loc>
        <lastmod>2006-12-12</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>
</urlset>

我在浏览器中收到以下错误页面 (http://localhost:50831/Sitemap)

This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

我已从 View(Sitemap.cshtml) 中删除所有空格。但还是报错

我什至尝试过this 并反转如下所示的顺序,但仍然出错。

<?xml version='1.0' encoding='UTF-8' ?>
@{    Layout = null;}
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
    <url>
        <loc>https://www.mywebsitename.com/home</loc>
        <lastmod>2006-12-12</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>
</urlset>

似乎它在导致上述错误的标题之前发送空白空间或行。 布局为空,视图以 xml 标记开始,并且从那里获得空白空间。

页面源输出

----EMPTY LINE----
        <?xml version='1.0' encoding='UTF-8' ?>
        <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
        http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
            <url>
                <loc>https://www.mywebsitename.com/home</loc>
                <lastmod>2006-12-12</lastmod>
                <changefreq>weekly</changefreq>
                <priority>1.00</priority>
            </url>
        </urlset>

我无法理解为什么?有什么想法吗?

我检查了以下但仍然无法找到答案

How to create XML sitemap programmatically in c#

sitemaps.xml in asp.net mvc

ASP.NET Web.sitemap to Generate sitemap.xml

【问题讨论】:

  • 你得到的实际输出是什么样的?
  • @Svish 使用输出源修改

标签: c# asp.net razor asp.net-mvc-5


【解决方案1】:

我通过在 Response.Write 中添加 XML 标记解决了这个问题

@{   
    Response.Write("<?xml version='1.0' encoding='UTF-8' ?>");
    Layout = null;
    }
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
    <url>
        <loc>https://www.mywebsitename.com/home</loc>
        <lastmod>2006-12-12</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>
</urlset>

感谢EvanSwd

【讨论】:

  • 请使用您问题上的编辑链接添加其他信息。 “发布答案”按钮应仅用于问题的完整答案。
  • @icebat 答案应该在答案部分,无论是完整的还是部分的。问题部分应仅包含有关问题而非答案的附加信息。
猜你喜欢
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多