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文件生成成功!"); } }