.net 获取“”;mvc站点地图提供程序在sitemap.xml的开头

.net 获取“”;mvc站点地图提供程序在sitemap.xml的开头,.net,asp.net-mvc,mvcsitemapprovider,.net,Asp.net Mvc,Mvcsitemapprovider,如何在没有BOM的情况下以编码utf-8的方式生成站点地图?创建一个类来覆盖XmlSiteMapResult的默认行为,以便它排除BOM public class HomeController : Controller { public ActionResult SitemapXML() { return new XmlSiteMapResult(); } } 注意:本例假设您使用的是MvcSiteMapProvider的v3,因为在v4中,XmlSit

如何在没有BOM的情况下以编码utf-8的方式生成站点地图?

创建一个类来覆盖XmlSiteMapResult的默认行为,以便它排除BOM

public class HomeController : Controller
{
    public ActionResult SitemapXML()
    {
        return new XmlSiteMapResult();
    }
}
注意:本例假设您使用的是MvcSiteMapProvider的v3,因为在v4中,XmlSiteMapResult类不再具有默认构造函数(在v4中,必须使用XmlSiteMapResultFactory获取XmlSiteMapResult的实例,因此还需要重写XmlSiteMapResultFactory以返回自定义类的实例)


参考资料:

为什么BOM是一个问题?任何有效的XML读取器都应该能够处理它。validation XML yandex.ru,find Error听起来像是坏了……或者您的响应编码设置不正确。我建议您查看您得到的确切响应,包括标题。检查
public class MyXmlSiteMapResult : XmlSiteMapResult
{

    /// <summary>
    /// Maximal number of links per sitemap file.
    /// </summary>
    /// <remarks>
    /// This number should be 50000 in theory, see http://www.sitemaps.org/protocol.php#sitemapIndex_sitemap.
    /// Since sitemap files can be maximal 10MB per file and calculating the total sitemap size would degrade performance,
    /// an average cap of 35000 has been chosen.
    /// </remarks>
    private const int MaxNumberOfLinksPerFile = 35000;

    protected override void ExecuteSitemapIndexResult(ControllerContext context, IEnumerable<SiteMapNode> flattenedHierarchy, long flattenedHierarchyCount)
    {
        // Count the number of pages
        double numPages = Math.Ceiling((double)flattenedHierarchyCount / MaxNumberOfLinksPerFile);

        // Output content type
        context.HttpContext.Response.ContentType = "text/xml";

        // Generate sitemap sitemapindex
        var sitemapIndex = new XElement(Ns + "sitemapindex");
        sitemapIndex.Add(GenerateSiteMapIndexElements(Convert.ToInt32(numPages), Url, SiteMapUrlTemplate).ToArray());

        // Generate sitemap
        var xmlSiteMap = new XDocument(
            new XDeclaration("1.0", "utf-8", "true"),
            sitemapIndex);

        // Specify to emit XML with no BOM
        var settings = new XmlWriterSettings();
        settings.Encoding = new System.Text.UTF8Encoding(false);

        // Write XML
        using (Stream outputStream = RetrieveOutputStream(context))
        {
            using (var writer = XmlWriter.Create(outputStream, settings))
            {
                xmlSiteMap.WriteTo(writer);
            }
            outputStream.Flush();
        }
    }

    protected override void ExecuteSitemapResult(ControllerContext context, IEnumerable<SiteMapNode> flattenedHierarchy, long flattenedHierarchyCount, int page)
    {

        // Output content type
        context.HttpContext.Response.ContentType = "text/xml";

        // Generate URL set
        var urlSet = new XElement(Ns + "urlset");
        urlSet.Add(GenerateUrlElements(
            flattenedHierarchy.Skip((page - 1)* MaxNumberOfLinksPerFile)
                .Take(MaxNumberOfLinksPerFile), Url).ToArray());

        // Generate sitemap
        var xmlSiteMap = new XDocument(
            new XDeclaration("1.0", "utf-8", "true"),
            urlSet);

        // Specify to emit XML with no BOM
        var settings = new XmlWriterSettings();
        settings.Encoding = new System.Text.UTF8Encoding(false);

        // Write XML
        using (Stream outputStream = RetrieveOutputStream(context))
        {
            using (var writer = XmlWriter.Create(outputStream, settings))
            {
                xmlSiteMap.WriteTo(writer);
            }
            outputStream.Flush();
        }
    }
}
public class HomeController : Controller
{
    public ActionResult SitemapXML()
    {
        return new MyXmlSiteMapResult();
    }
}