mvc sitemap provider在sitemap.xml的开头获取“”

时间:2013-09-26 11:07:59

标签: .net asp.net-mvc mvcsitemapprovider

public class HomeController : Controller
{
    public ActionResult SitemapXML()
    {
        return new XmlSiteMapResult();
    }
}

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

1 个答案:

答案 0 :(得分:0)

创建一个类来覆盖XmlSiteMapResult的默认行为,因此它会排除BOM。

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

注意:此示例假设您使用的是MvcSiteMapProvider的v3,因为在v4中,XmlSiteMapResult类不再具有默认构造函数(在v4中,必须使用XmlSiteMapResultFactory来获取XmlSiteMapResult的实例所以你还需要覆盖XmlSiteMapResultFactory来返回你的自定义类的实例。

参考:How can I remove the BOM from XmlTextWriter using C#?