2

我需要生成一个站点地图以使用 Google 网站管理员工具验证该站点。

如何自动为我的网站生成站点地图?

4

2 回答 2

3

试试这个示例:

@using System.Xml.Linq;
@{
    var urls = new List<string>{"home", "about", "contact"};
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    var baseurl = "http://www.domain.com/{0}";
    var sitemap = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
            new XElement(ns + "urlset",
                from url in urls select
                new XElement("url",
                    new XElement("loc", string.Format(baseurl, url)),
                    new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
                    new XElement("changefreq", "monthly"),
                    new XElement("priority", "0.5")
                    )
                )
            );
    Response.ContentType = "text/xml";
    sitemap.Save(Response.Output);
}

将文件另存为 Sitemap.cshtml。显然,您需要将列表替换为地图的位置源。但至少您可以看到 XML 是如何生成的。

于 2011-05-13T04:34:59.193 回答
2

我认为这是你最好的选择:

ASP.NET MVC 站点地图提供程序

阅读这些页面:

动态站点地图

导出站点地图以进行搜索引擎索引

于 2011-05-13T03:17:01.690 回答