26 sitemap asp.net asp.net-mvc
我想知道是否有人已经这样做了,或者有关于如何为MVC网站创建Google Sitemap的任何示例.
任何帮助或示例将不胜感激.
我正在谈论这个:https: //www.google.com/webmasters/tools/docs/en/protocol.html
Jer*_*ade 22
我使用了Mike Brind的Sitemap代码,只做了一些小改动.
您需要将XNamespace添加到每个XElement,否则Google会吐出虚拟对象.
这是我的版本:
public ContentResult Index()
{
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
const string url = "http://www.website.com/controller/action/{0}";
var items = _db.DataAccessHere();
var sitemap = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(ns + "urlset",
from i in items
select
//Add ns to every element.
new XElement(ns + "url",
new XElement(ns + "loc", string.Format(url, i.ItemID)),
new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", i.DateAddedUTC)),
new XElement(ns + "changefreq", "monthly"),
new XElement(ns + "priority", "0.5")
)
)
);
return Content(sitemap.ToString(), "text/xml");
}
Run Code Online (Sandbox Code Playgroud)
感谢Mike发布原始文章和代码.
无耻的自我插件:在生产中遇到与MvcSiteMapProvider有关的奇怪问题后,我创建了一个名为SimpleMvcSitemap的库.您可以从任何操作方法提供站点地图文件,而无需任何配置:
public class SitemapController : Controller
{
public ActionResult Index()
{
List<SitemapNode> nodes = new List<SitemapNode>
{
new SitemapNode(Url.Action("Index","Home")),
new SitemapNode(Url.Action("About","Home")),
//other nodes
};
return new SitemapProvider().CreateSitemap(nodes);
}
}
Run Code Online (Sandbox Code Playgroud)
它还支持所有可用的Google Sitemap扩展.