新闻系统中为了减轻服务器的负担。有时需要把数据库内容不是动态读出,
而是写成静态的网页。
以下是写html。更新html,和删除html的代码。
工程中Conn.cs是公共调用的一个类库。(看梦凌早期写的论坛Access版学来的。)
//生成HTML页
public
static
bool
WriteFile(
string
strText,
string
strContent,
string
strAuthor,
string
filename)
... {
string yy=DateTime.Now.ToString("yyyy");
string dd=DateTime.Now.ToString("dd");
string mm=DateTime.Now.ToString("MM");
string path = HttpContext.Current.Server.MapPath("/news/"+yy.ToString()+"/"+mm.ToString()+"/"+dd.ToString()+"/");
string addtime=DateTime.Now.ToString();
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
...{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("HHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$Title$",strText);
str = str.Replace("$filename$",filename);
str = str.Replace("$Content$",strContent);
str = str.Replace("$Author$",strAuthor);
str = str.Replace("$AddTime$",addtime);
// 写文件
if(!System.IO.Directory.Exists(path))
...{
System.IO.Directory.CreateDirectory(path);
try
...{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
}
return true;
}
else
...{
try
...{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
sr.Close();
}
return true;
}
}
// 修改HTML页
public static bool ModifyFile( string strText, string strContent, string strAuthor, string htmlfilename)
... {
Encoding code = Encoding.GetEncoding("gb2312");
string path=HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
...{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string addtime=DateTime.Now.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$Title$",strText);
str = str.Replace("$Content$",strContent);
str = str.Replace("$Author$",strAuthor);
str = str.Replace("$AddTime$",addtime);
// 写文件
try
...{
sw = new StreamWriter(path , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
sr.Close();
}
return true;
}
// 删降HTML
public static void Delete( string htmlfilename)
... {
string path = HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
try
...{
File.Delete(path);
}
catch (Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
}
}
... {
string yy=DateTime.Now.ToString("yyyy");
string dd=DateTime.Now.ToString("dd");
string mm=DateTime.Now.ToString("MM");
string path = HttpContext.Current.Server.MapPath("/news/"+yy.ToString()+"/"+mm.ToString()+"/"+dd.ToString()+"/");
string addtime=DateTime.Now.ToString();
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
...{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("HHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$Title$",strText);
str = str.Replace("$filename$",filename);
str = str.Replace("$Content$",strContent);
str = str.Replace("$Author$",strAuthor);
str = str.Replace("$AddTime$",addtime);
// 写文件
if(!System.IO.Directory.Exists(path))
...{
System.IO.Directory.CreateDirectory(path);
try
...{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
}
return true;
}
else
...{
try
...{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
sr.Close();
}
return true;
}
}
// 修改HTML页
public static bool ModifyFile( string strText, string strContent, string strAuthor, string htmlfilename)
... {
Encoding code = Encoding.GetEncoding("gb2312");
string path=HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
...{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string addtime=DateTime.Now.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$Title$",strText);
str = str.Replace("$Content$",strContent);
str = str.Replace("$Author$",strAuthor);
str = str.Replace("$AddTime$",addtime);
// 写文件
try
...{
sw = new StreamWriter(path , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
sr.Close();
}
return true;
}
// 删降HTML
public static void Delete( string htmlfilename)
... {
string path = HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
try
...{
File.Delete(path);
}
catch (Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
}
}
生成了静态的html页如何实现点击率呢。这是个难题看来asp版的
http://www.knowsky.com/3439.html
按照此文的思路。试了试,关于如何读出新增ID的值,这里还是个难点。暂不理会
<SCRIPT src="counter.asp?articleId=<%=#articleId#%>"></SCRIPT >
我换用了数据库中FileName这个存放生成文件名的字段。变成
<script src="../../../../article/counter.aspx?filename=$filename$"></script>
因为生成的html页是按年/月/份/时间.html生成的所以上面用到相对路径。
这样<script src="../../../../article/counter.aspx?filename=2004/05/16/191640.html">
传给counter.aspx的filename参数就是数据库对应的filename字段 根据传过来的filename
然后更新数据库中的click字段,再读出来。
注:此counter.aspx的ASPX文件就只有以下的代码
<%@ Page language="c#" Codebehind="Counter.aspx.cs" AutoEventWireup="false" Inherits="Hover.Article.Counter" %>
具体什么原因,请看这里 http://www.cnblogs.com/hover/archive/2004/04/30/8191.aspx
以下是counter.aspx的代码。
using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Hover.Article
... {
/**////<summary>
/// Counter 的摘要说明。
///</summary>
public class Counter : System.Web.UI.Page
...{
protected Hover.Conn obj=new Hover.Conn();
public DataRow dr;
public String filename;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
filename=Request.Params["filename"];
if (!this.Page.IsPostBack)
...{
if (filename!=null)
...{
UpdateClick(filename);
}
else
...{
Response.Write ("无参数传递错误!");
}
this.ReadClick(filename);
}
}
public void UpdateClick(string filename)
...{
try
...{
OleDbDataReader rs=null;
obj.open1();
rs=obj.ccmd("select click from news where filename='"+filename+"'").ExecuteReader();
rs.Read();
int i = rs.GetInt32(0);
i++;
rs.Close();
obj.ccmd("UPDATE news SET click = "+i.ToString()+" WHERE filename='"+filename+"'").ExecuteNonQuery();
obj.link.Close();
}
catch(Exception ex)
...{
Response.Write(2+ex.Message );
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////<summary>
///设计器支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
///</summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/**////<summary>
///读click数
///</summary>
public void ReadClick(string filename)
...{
string Sql="select click from news where filename='"+filename+"'";
try
...{
obj.open1();
OleDbDataAdapter da=new OleDbDataAdapter(Sql,obj.link);
DataSet ds=new DataSet();
da.Fill(ds,"RecordClick");
dr = ds.Tables["RecordClick"].Rows[0];
string filename1=dr["click"].ToString();
Response.Write("document.write('"+filename1.ToString()+"')") ;
obj.link.Close();
}
catch(Exception ex)
...{
Response.Write(ex.Message );
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Hover.Article
... {
/**////<summary>
/// Counter 的摘要说明。
///</summary>
public class Counter : System.Web.UI.Page
...{
protected Hover.Conn obj=new Hover.Conn();
public DataRow dr;
public String filename;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
filename=Request.Params["filename"];
if (!this.Page.IsPostBack)
...{
if (filename!=null)
...{
UpdateClick(filename);
}
else
...{
Response.Write ("无参数传递错误!");
}
this.ReadClick(filename);
}
}
public void UpdateClick(string filename)
...{
try
...{
OleDbDataReader rs=null;
obj.open1();
rs=obj.ccmd("select click from news where filename='"+filename+"'").ExecuteReader();
rs.Read();
int i = rs.GetInt32(0);
i++;
rs.Close();
obj.ccmd("UPDATE news SET click = "+i.ToString()+" WHERE filename='"+filename+"'").ExecuteNonQuery();
obj.link.Close();
}
catch(Exception ex)
...{
Response.Write(2+ex.Message );
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////<summary>
///设计器支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
///</summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/**////<summary>
///读click数
///</summary>
public void ReadClick(string filename)
...{
string Sql="select click from news where filename='"+filename+"'";
try
...{
obj.open1();
OleDbDataAdapter da=new OleDbDataAdapter(Sql,obj.link);
DataSet ds=new DataSet();
da.Fill(ds,"RecordClick");
dr = ds.Tables["RecordClick"].Rows[0];
string filename1=dr["click"].ToString();
Response.Write("document.write('"+filename1.ToString()+"')") ;
obj.link.Close();
}
catch(Exception ex)
...{
Response.Write(ex.Message );
}
}
}
}