PageAdmin Cms自助建站系統生成百度SiteMap文件的方法
- 2019 年 10 月 31 日
- 筆記
PageAdmin Cms作為一款優秀的自助建站系統,在國內擁有不少的用戶,之前在論壇里看到很多用戶生成百度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文件生成成功!"); } }