0
public class HomeController : Controller
{
public ActionResult SitemapXML()
{
return new XmlSiteMapResult();
}
}
如何生成编码UTF-8无地图BOM?获得“我»¿”在sitemap.xml的年初通过MVC网站地图提供商
public class HomeController : Controller
{
public ActionResult SitemapXML()
{
return new XmlSiteMapResult();
}
}
如何生成编码UTF-8无地图BOM?获得“我»¿”在sitemap.xml的年初通过MVC网站地图提供商
创建一个类来覆盖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类不再有默认的构造函数(在第4版,您必须使用XmlSiteMapResultFactory获得XmlSiteMapResult的一个实例,这样您还需要重写XmlSiteMapResultFactory以返回您的自定义类的实例)。
为什么BOM有问题?任何有效的XML阅读器都应该能够应付它。 –
验证XML yandex.ru,发现错误 – konstantin
听起来像是一个坏然后......或者你的响应编码设置不正确。我建议你看*完全*你会得到什么回应,包括标题。 –