【发布时间】:2013-12-23 23:49:17
【问题描述】:
我的站点地图生成有以下类:
public class SitemapItem
{
public SitemapItem(string url)
{
this.Url = url;
this.AlternateLinks = new List<SiteMapAlternateLink>();
}
public string Url { get; set; }
public DateTime? LastModified { get; set; }
public ChangeFrequency? ChangeFrequency { get; set; }
public float? Priority { get; set; }
public List<SiteMapAlternateLink> AlternateLinks { get; set; }
}
还有:
public class SiteMapAlternateLink
{
public SiteMapAlternateLink(string url, string language)
{
this.Url = url;
this.Language = language;
}
public string Url { get; set; }
public string Language { get; set; }
}
现在在我的控制器中,我填写一个 SitemapItems 列表并使用以下代码从控制器返回它:
public class XmlSitemapResult : ActionResult
{
private XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
private XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";
private IEnumerable<SitemapItem> _items;
public XmlSitemapResult(IEnumerable<SitemapItem> items)
{
_items = items;
}
public override void ExecuteResult(ControllerContext context)
{
string encoding = context.HttpContext.Response.ContentEncoding.WebName;
XDocument sitemap = new XDocument(new XDeclaration("1.0", encoding, "yes"),
new XElement(nsSitemap + "urlset", new XAttribute(XNamespace.Xmlns + "xhtml", nsXhtml),
from item in _items
select CreateItemElement(item)
)
);
context.HttpContext.Response.ContentType = "application/xml";
context.HttpContext.Response.Charset = encoding;
context.HttpContext.Response.Flush();
context.HttpContext.Response.Write(sitemap.Declaration + sitemap.ToString());
}
private XElement CreateItemElement(SitemapItem item)
{
XElement itemElement = new XElement(nsSitemap + "url", new XElement(nsSitemap + "loc", item.Url.ToLower()));
if (item.LastModified.HasValue)
itemElement.Add(new XElement(nsSitemap + "lastmod", item.LastModified.Value.ToString("yyyy-MM-dd")));
if (item.ChangeFrequency.HasValue)
itemElement.Add(new XElement(nsSitemap + "changefreq", item.ChangeFrequency.Value.ToString().ToLower()));
if (item.Priority.HasValue)
itemElement.Add(new XElement(nsSitemap + "priority", item.Priority.Value.ToString(CultureInfo.InvariantCulture)));
foreach (var alternateLink in item.AlternateLinks)
{
itemElement.Add(new XElement(nsXhtml + "link",
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", alternateLink.Language),
new XAttribute("href", alternateLink.Url)));
}
return itemElement;
}
}
现在的问题是在我的浏览器中我不会将 XML 视为 XML。我只会看到标准 XML 查看器无法查看的文本。这发生在所有浏览器中,并且似乎从我添加 xhtml 架构的那一刻起就发生了。
希望有人看到问题,提前谢谢!
编辑:如果我删除所有与 xhtml 相关的内容,浏览器会将其显示为 xml。有什么想法吗?
EDIT2: html:
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:11149/en</loc>
<changefreq>hourly</changefreq>
<priority>0.6</priority>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl"/>
</url>
<url>
<loc>http://localhost:11149/en/buyandsell</loc>
<changefreq>weekly</changefreq>
<priority>1</priority>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/BuyAndSell"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/BuyAndSell"/>
</url>
<url>
<loc>http://localhost:11149/en/partner</loc>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/Partner"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/Partner"/>
</url>
<url>
<loc>http://localhost:11149/en/news</loc>
<lastmod>2013-12-06</lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
<xhtml:link rel="alternate" hreflang="en" href="http://localhost:11149/en/News"/>
<xhtml:link rel="alternate" hreflang="nl" href="http://localhost:11149/nl/News"/>
</url>
</urlset>
【问题讨论】:
-
你需要显示你的控制器方法。还有原始 HTTP 是什么样的?
-
@Liam 我添加了 html,控制器与它无关,因为它返回一个 XmlSiteMapResult,除了填充项目列表之外没有任何反应。
-
如果您查看站点地图 (xdocument),它看起来是否像格式良好的 XML?
-
根据 MSDN,这是一个有效的站点地图:msdn.microsoft.com/en-us/library/yy2ykkab(v=vs.100).aspx
-
确实,只要我让网站验证站点地图,它就会完美验证。虽然我希望每当客户访问站点地图时,他都会使用浏览器 xml 查看器看到它。
标签: c# asp.net xml asp.net-mvc sitemap