blueyund

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在文章发布系统中采用服务器端生成静态页面的方法可以有效减轻服务器的负担,特别是对大流量网站非常有效。但是既然生成的是静态页面,生成时是什么样,显示就是什么样了,对于文章常见文章被阅读次数怎么显示呢?

例见:ASP.Net生成静态HTML页!

看了ASP版的http://www.knowsky.com/3439.html 试着实现了一把没调通!哎!够笨
就想自己想个办法解决吧!想找找类似方案,无意中看了活靶子.Net
的AspxBoy.com 。如此篇文章 http://www.aspxboy.com/ShowArticle.Aspx?ID=273 打开后却是跳转到
此地址:http://www.aspxboy.com/Files/71/66/2004325112351.htm
给我了启发可以通过点击ID然后把新闻中记录点击率的字段如click加1然后再写回已生成的页面!
把显示新闻标题的链接改成下面的了原来的是链接到生成的html页.
 <asp:HyperLink runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.title") %>' ForeColor=#000000 Font-Bold =True Font-Size=12 ID="HyperLink1" ToolTip='单击查看详细信息' NavigateUrl='<%# "../ShowArticle.aspx?id=" + DataBinder.Eval(Container.DataItem,"id").ToString()%>' Target=_blank></asp:HyperLink>

新建一个WebForm ShowArticle.Aspx以下是cs 代码
--------------------------start ShowArticle.Aspx.CS------------------------

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

{

     /// <summary>

     /// ShowArticle 的摘要说明。

     /// </summary>

     public class ShowArticle : System.Web.UI.Page

     {

         protected Hover.Conn obj=new Hover.Conn();

         public DataRow dr;

         public String id;

         private void Page_Load(object sender, System.EventArgs e)

         {

              id=Request.Params["id"];   

              if (id!=null)

              {

                   if (!obj.checkid(id))

                   {

                       Response.Write("<script language='javascript'>alert('非法参数!');location.href='../Default.Aspx';</script>");

                       Response.End();

                   }

                   else

                   {

                       UpdateClick(id);

                       Redirect(id);

                   }

              }

              else

              {

                   Response.Write ("无参数传递错误!");

              }

       

         }

         #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>

         /// 跳转到Html页

         /// </summary>

         public void Redirect(string id)

         {

              string Sql="Select title,content,author,filename,click from news where id= "+id;

              try

              {

                   obj.open();

                   OleDbDataAdapter da=new OleDbDataAdapter(Sql,obj.link);

                   DataSet ds=new DataSet();

                   da.Fill(ds,"RedirectTable");

                   dr = ds.Tables["RedirectTable"].Rows[0];

                   string filename=dr["filename"].ToString();

                   string click=dr["click"].ToString();

                   string title=dr["title"].ToString();

                   string content=dr["content"].ToString();

                   string author=dr["author"].ToString();

                   if (Hover.Conn.WriteCounter(title,content,author,click,filename))

                   {

                       Response.Redirect("./news/"+filename);

                       Response.End();

                   }

                   else

                   {

                       Response.Write("更新点击率出错");

                       Response.End();

                   }

                  

                   obj.link.Close();;

              }

              catch(Exception ex)

              {

                   Response.Write(ex.Message );

              }

         }

 

         /// <summary>

         /// 更新点击率方法

         /// </summary>

         public void UpdateClick(string id)

         {

              try

              {

                   OleDbDataReader rs=null;

                   obj.open();

                   rs=obj.ccmd("select click from news where id="+id).ExecuteReader();

                   rs.Read();

                   int i = rs.GetInt32(0);

                   i++;

                   rs.Close();

                   obj.ccmd("UPDATE news SET click = "+i.ToString()+" WHERE id= "+id).ExecuteNonQuery();

                   obj.link.Close();

              }

              catch(Exception ex)

              {

                   Response.Write(ex.Message );

              }

         }

 

 

     }   

}

 

--------------------------------------End ShowArticle.aspx.cs-----------------------------------
以下是ShowArticle.aspx.cs中调用Hover.Conn的类的代码
---------------------start conn.cs----------------------------------
//并非全部conn.cs代码,只列出了ShowArticle.aspx调用到的代码

public static bool WriteCounter(string strText,string strContent,string strAuthor,string click, string filename)

         {

              Encoding code = Encoding.GetEncoding("gb2312");

              string path=HttpContext.Current.Server.MapPath("/news/"+filename);

              // 读取模板文件

              string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");

              //string temp = HttpContext.Current.Server.MapPath("/news/"+filename);

       //这里有个疑问.我用string temp = HttpContext.Current.Server.MapPath("/news/"+filename);
              //这是选择模板时选择已生成的文件利用它做模板,这样的话下面的替换str变量我就只写
             //str = str.Replace("$ClickCounter$",click);这一句就够了,但选择已生成的文件做模板总报该进程无法访问文
  //件“D:"Hover"news"2004"04"21"20040421215951.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("$ClickCounter$",click);

              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();

              }

              sw.Close();

              sr.Close();

              return true;      

         }

//验证输入id是否为非法参数

         public bool checkid(string str)

         {

 

              try

              {

                   int i=int.Parse(str); 

              }

              catch

              {

                   return false;

              }

              return true;

         }

---------------------------end conn.cs---------------------------------
终于自己解决了一个问题。记录一下。哪位兄弟有别的方法实现一起交流一下,再有
上面红色字的部分。哪位讲一下,我还是不很清楚呀!
附演示地址:http://bns.23city.com/

posted on 2008-07-28 08:00  blueyund  阅读(259)  评论(0编辑  收藏  举报