Asp.net 静态页面生成(1)----模板生成

 废话少说,开门见山。静态页面生成技术是很多大网站采用的技术。用于大型网站的访问量特别高,采用动态页面难免服务器负担很重,负载大的时候可能down机。人们想出了采取以前的静态页面,这样可以减少服务器运算负载,但是一个一个页面的做肯定人工成本太高。于是乎就出现了静态页面生成技术。
      静态页面生成的实现方法大致可以分为两种
      一、模板生成
      实现思想:提前把网页的公共部分写好,做成一个模板,而不同的部分采用特殊字符代替。当需要生产的时候,用程序去读取模板,然后去数据库中找到需要的数据替换模板中的特殊字符。生成真正的网页然后存在网站的目录下。  

      有点:效率高,生成速度快。
      缺点:需要在网站建设之前就确定采用此方法生成。对于已建好的动态站点改版为此方法 工作量大。
      实例代码:
Article.Templete.txt

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
    
< title >< @Title @ ></ title >
</ head >
< body >
    
< table  border ="0"  cellpadding ="0"  cellspacing ="0"  width ="980" >
        
< tr >
            
< td  style ="height: 200px" >
                header
            
</ td >
        
</ tr >
        
< tr >
            
< td >
                
< table  border ="0"  cellpadding ="0"  cellspacing ="0"  width ="980" >
                    
< tr >
                        
< td >
                        
< @Title @ >
                        
</ td >
                    
</ tr >
                    
< tr >
                        
< td >
                        
< @Content @ >
                        
</ td >
                    
</ tr >
                    
< tr >
                        
< td >
                        剪辑:
< @Editor @ > &nbsp;&nbsp;&nbsp;&nbsp; 访问次数: < @Hits @ >
                        
</ td >
                    
</ tr >
                
</ table >
            
</ td >
        
</ tr >
        
< tr >
            
< td  style ="height: 100px" >
                footer
            
</ td >
        
</ tr >
    
</ table >
</ body >
</ html >


生成函数

/// <summary>
    
/// 生成文章方法
    
/// </summary>
    
/// <param name="templetefile">模板文件路径</param>
    
/// <param name="articleId">文章Id</param>

     public   void  MakeArticleFile( string  templetefile,  int  articleId)
    
{
        
string sqlStr = "SELECT * FROM Article WHERE ArticleId = " + articleId.ToString();
        DataSet ds 
= DataBase.ExecuteDataset(sqlStr);
        
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        
{
            DataRow dr 
= ds.Tables[0].Rows[0];
            
try
            
{
                System.IO.StreamReader sr 
= new System.IO.StreamReader(templetefile, System.Text.Encoding.UTF8);
                System.IO.StreamWriter sw 
= new System.IO.StreamWriter(Server.MapPath("/Article/" + dr["ArticleId"].ToString() + ".html"), false, System.Text.Encoding.UTF8);
                
string templete = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                templete 
= templete.Replace("<@Title@>", dr["Title"].ToString());
                templete 
= templete.Replace("<@Content@>", dr["Content"].ToString());
                templete 
= templete.Replace("<@Editor@>", dr["Editor"].ToString());
                templete 
= templete.Replace("<@Hits@>", dr["Hits"].ToString());
                sw.Write(templete);
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }

            
catch (Exception ex)
            
{
                Response.Write(ex.Message);
            }

        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值