简体   繁体   中英

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.

@{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 )

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

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#

sitemaps.xml in asp.net mvc

ASP.NET Web.sitemap to Generate sitemap.xml

I fixed the problem by adding XML tag in Response.Write

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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