呵呵,搜索了很久,寻找 c#关于 Google sitemaps的内容,都没有找到合适的代码可以拿来直接用,于是查资料拼凑了一个。
本代码的看点:
- 单文件实现,通过 @import 代替 cs 文件中 using的方法,这个查了很久才知道在aspx中 using变成了这个
- 设置文件返回类型(见下面代码的第一行)
- 借鉴别人RSS生成器的做法,用Repater实现GoogleSitemap的输出
除了上述看点,也就剩下一个:比较少的修改就可以拿来用到你的网站上!
附件代码如下:
<%@Page Language="C#" ContentType="text/xml"%>
<%@Import Namespace="System"%>
<%@Import Namespace="System.IO"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>
<%@Import Namespace="System.Web"%>
<%@Import Namespace="System.Web.Security"%>
<%@Import Namespace="System.Web.UI"%>
<%@Import Namespace="System.Web.UI.HtmlControls"%>
<%@Import Namespace="System.Web.UI.WebControls"%>
<%@Import Namespace="System.Text"%>
<%@Import Namespace="System.Text.RegularExpressions"%>
<%@Import Namespace="System.Xml"%>
<script language="C#" runat="server">
public string rssTitle = "Google Sitemaps";
protected void Page_Load(object sender, EventArgs e)
{
/*
TODO: 本文件的特点是 单文件 实现 Google sitemaps的生成
环境是 Microsoft.net Framework 2.0 sp1
在 web.config 配置 connectionString 或有自己的 dbHelper
string connectionString = WebConfigurationManager.AppSettings["connectionString"];
SqlConnection con = new SqlConnection(connectionString);
string sql = @"SELECT url, 'weekly' as changefreq FROM Section UNION SELECT url, 'monthly' as changefreq FROM Articles ";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataSet ds=new DataSet();
reader.Fill(ds);
*/
BLL.News newsbll = new BLL.News();
rptRss.DataSource = newsbll.GetList("deletetag=0 order by publishdate desc");
rptRss.DataBind();
/*
rptRss.DataSource = reader;
rptRss.DataBind();
*/
}
</script><head runat="server" visible="false"></head><asp:Repeater id="rptRss" runat="server">
<HeaderTemplate><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<!--<%# rssTitle %>--></HeaderTemplate>
<ItemTemplate>
<url><loc>http://www.dutemba.com/newsdetail.aspx?id=<%# Eval("id") %></loc><changefreq>monthly</changefreq><lastmod><%# string.Format("{0:s}", Eval("publishdate")) %>+08:00</lastmod></url></ItemTemplate>
<FooterTemplate>
</urlset>
</FooterTemplate>
</asp:Repeater>
呵呵,搜索了很久,寻找 c#关于 Google sitemaps的内容,都没有找到合适的代码可以拿来直接用,于是查资料拼凑了一个。