简体   繁体   English

ASP.Net C#MVC5 Razor查看XML站点地图

[英]ASP.Net C# MVC5 Razor View XML sitemap

My Controller Action looks like this. 我的控制器动作看起来像这样。

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

My View Sitemap.cshtml looks like this. 我的视图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>

I am getting following ERROR in browser for page ( http://localhost:50831/Sitemap ) 我在浏览器中获取ERROR页面( 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.

I have removed all spaces from View(Sitemap.cshtml). 我已从View(Sitemap.cshtml)中删除了所有空格。 but still error 但仍然是错误

I even tried this and inverse the order as shown below but still error. 我甚至尝试了这个并反转了如下所示的顺序,但仍然是错误。

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

It seems like it is sending empty space or line before header which causes above error. 它似乎是在标头之前发送空的空格或行,导致上述错误。 Layout is null, view starts with xml tag and from where it is getting empty space. 布局为空,视图以xml标记开始,并从中获取空白空间。

page source output 页面源输出

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

I could not get my head around why? 我无法理解为什么? Any Idea? 任何想法?

I checked following but still could not able to find answer 我检查了以下但仍无法找到答案

How to create XML sitemap programmatically in c# 如何在c#中以编程方式创建XML站点地图

sitemaps.xml in asp.net mvc asp.net mvc中的sitemaps.xml

ASP.NET Web.sitemap to Generate sitemap.xml ASP.NET Web.sitemap生成sitemap.xml

I fixed the problem by adding XML tag in Response.Write 我通过在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>

Thanks to EvanSwd 感谢EvanSwd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM