PageAdmin作為一款優秀的建站系統,國内擁有不少的使用者,之前在論壇裡看到很多使用者生成百度SiteMap檔案都是通過安裝插件來實作,但實際上通過系統自帶的自定義路由功能一樣可以實作siteMap檔案生成,下面說一下步驟。
1、首先添加一個自定義頁面配置,如何添加自定義路由,請參考我之前的文章,或者到官方幫助中搜尋:自定義頁面,可以找到自定義路由的使用方法,下面是我添加的一個配置。
<route urlConstraint="^buildSiteMap.cshtml$" viewPath="siteMap/siteMap.cshtml" httpcacheSolutionId="0" title=""></route>
以上配置僅供參考,大家可以根據自己需要來寫。
2、在模闆目錄的Views目錄下新一個siteMap/siteMap.cshtml檔案,檔案内容如下:
@{
Layout = null;
string table = Request.QueryString["table"];
string domain = "http://localhost:800/buildSiteMap.cshtml";//localhost:800改為您的網站域名,必須是外網域名
//生成欄目siteMap
if (table=="column")
{
<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">
@foreach (var item in Html.GetColumnList().Where(c => c.Show == 1 && c.ColumnType <= 2))
{
string url = Html.ColumnUrl((int)(item.Id));
<url>
<mobile:mobile type="pc,mobile" />
<loc>@url</loc>
<lastmod>@DateTime.Now.ToString("yyyy-MM-dd")</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
}
</urlset>
}
//生成資訊表的siteMap
else if (!string.IsNullOrEmpty(table))
{
<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach (var item in Html.InfoDataList(new { Table = table, ShowNumber = 1500 }))
{
<url>
<mobile:mobile type="pc,mobile" />
<loc>http://www.pageadmin.net/jianzhan/@(item.Id).cshtml</loc>
<lastmod>@item.Thedate.ToString("yyyy-MM-dd")</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
}
</urlset>
}
else
{
HttpRequestHelper httpRequestHelper = new HttpRequestHelper();
//生成欄目siteMap檔案
IOHelper.CreateFile("/columnSiteMap.xml", httpRequestHelper.Get(domain+"?table=column").Trim(), true);
//生成product表的siteMap檔案,必須保證news資訊表實際存在
IOHelper.CreateFile("/productSiteMap.xml", httpRequestHelper.Get(domain+"?table=product").Trim(), true);
//生成news表的siteMap檔案,必須保證news資訊表實際存在
IOHelper.CreateFile("/newsSiteMap.xml", httpRequestHelper.Get(domain+"?table=news").Trim(), true);
//更多資訊表可以自行添加IOHelper.CreateFile方法,table參數改為資訊表名即可
Response.Write("sitemap檔案生成成功!");
}
}
3、最後直接在浏覽器中輸入:您的域名/buildSiteMap.cshtml 就會自動生成對應siteMap.xml檔案,生成後在百度官方的站長工具送出即可。