【发布时间】:2009-04-07 23:17:46
【问题描述】:
我正在尝试创建一个输出有效 sitemap.xml 文件的自动站点地图 ActionResult。文件的实际生成不是问题,但我似乎无法弄清楚如何填充系统中的 URL 列表。这是我到目前为止的代码:
public ContentResult Sitemap()
{
XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XElement root = new XElement(xmlns + "urlset");
//some kind of foreach here to get the loc variable for all URLs in the site
//for each URL in the collection, add it to the root element as here
//root.Add(
// new XElement("url",
// new XElement("loc", "http://google.com"),
// new XElement("changefreq", "daily")));
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(ms, Encoding.UTF8))
{
root.Save(writer);
}
return Content(Encoding.UTF8.GetString(ms.ToArray()), "text/xml", Encoding.UTF8);
}
}
例如,假设我有两个控制器,每个控制器都有两个与之关联的操作:
帮助控制器
- 编辑
- 创建
关于控制器
- 公司
- 管理
我似乎无法弄清楚如何获取 URL 列表:
【问题讨论】:
-
最近,像@eduncan911的回答一样,最好的解决方案是使用mvcsitemap.codeplex.com活动和更新项目,支持安全修剪并生成sitemap.xml。如果代理支持它,它还可以自动压缩站点地图,如果站点太大,它可以将站点地图拆分为子站点地图,因为 sitemap.xml 标准仅限于 50k 节点。
-
感谢 CallMeLaNN。我最近更新了答案,列出了这些要点以及更多内容,并列出了它移动到的网站。
标签: asp.net-mvc