如何在Asp.net MVC中实现站点地图?

时间:2016-11-22 08:11:10

标签: asp.net asp.net-mvc-4

任何人都可以指导我更新我们如何在Asp.net MVC网站上实施站点地图 提前致谢

1 个答案:

答案 0 :(得分:1)

使用可以使用System.Xml.Linq

public ContentResult GetSiteMap()
{
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    return new XElement(ns + "urlset",
        new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
        new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
        from node in db.MyTable()
        select new XElement(ns + "url",
            new XElement(ns + "loc", node.Loc),
            new XElement(ns + "lastmod", node.LastMod),
            new XElement(ns + "priority", node.Priority)
        )
    ).ToString();
    return Content(xsi.ToString(), "text/xml"); 
}