我们将从PHP模板引擎技术谈谈ASP.NET模板引擎技术,希望通过本文的实例和代码,能让大家在今后的开发过程中更加灵活的运用ASP.NET模板引擎技术。

以前听我朋友说起php的模板引擎技术的时候似懂非懂哪时感觉真的很强,一直在想asp.net有这种技术吗?我不知道我的理解是不是对的.其实 asp.net模板引擎技术就是先建好一个静态的html页面我们称它为模板页,你如果有不同形式的页面哪就得建立不同的静态模板页,然后在后台用文件操 作往这个文件里写东西然后在把这个模板页另存到一个静态页面的目录,不好意思可能我的理解太俗,如果有更好的理解和想法可以在apolov发文章告诉我谢 谢。现在我附加一下代码

Default.aspx这个页面只有几个textbox控件和两个按妞控件

  1.     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" ValidateRequest="false" Inherits="ToHtml._Default" %>   
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3.     <html xmlns="http://www.w3.org/1999/xhtml" >   
  4.     <head runat="server">   
  5.         <title>Asp.net生成静态页</title>   
  6.     </head>   
  7.     <body>   
  8.         <form id="form1" runat="server">   
  9.         <div>   
  10.             标题:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />   
  11.             内容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"   
  12.                 Width="350px"></asp:TextBox><br />   
  13.             <br />   
  14.             <asp:Button ID="Button1" runat="server" onClick="Button1_Click" Text="根据模板生成" /><br />   
  15.             <br />   
  16.             <br />   
  17.             Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="请确认Url地址的存在" Width="359px"></asp:TextBox>   
  18.             <br />   
  19.             <br />   
  20.             <asp:Button ID="Button2" runat="server" Text="根据Url地址生成" onClick="Button2_Click" /></div>   
  21.         </form>   
  22.     </body>   
  23.     </html>   

要准备的模板页代码,htm文件页面比较简单,如果有兴趣的朋友可以做成更复杂的模板页嘿嘿


  1.     !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2.     <html xmlns="http://www.w3.org/1999/xhtml" >   
  3.     <head>   
  4.         <title> $title$ 生成静态页title>   
  5.         <style type="text/css">   
  6.     <!--    
  7.     .STYLE1 {    
  8.      font-size: 16px;    
  9.      font-weight: bold;    
  10.     }    
  11.     -->   
  12.         </style>   
  13.     </head>   
  14.     <body>   
  15.     <br />   
  16.     <br />   
  17.     <table width="100%" border="0" bgcolor="#339900">   
  18.       <tr>   
  19.         <td height="34" align="center" bgcolor="#FFFFFF"><span class="STYLE1">$title$ </span></td>   
  20.       </tr>   
  21.       <tr>   
  22.         <td height="42" bgcolor="#FFFFFF"><br />   
  23.           <br />   
  24.         内容:$content$ </td>   
  25.       </tr>   
  26.     </table>   
  27.        
  28.     </body>   
  29.     </html>   

后台生成静态页面的代码Default.aspx.cs主要用到了文件操做


  1.     sing System;    
  2.     using System.Data;    
  3.     using System.Configuration;    
  4.     using System.Web;    
  5.     using System.Web.Security;    
  6.     using System.Web.UI;    
  7.     using System.Web.UI.WebControls;    
  8.     using System.Web.UI.WebControls.WebParts;    
  9.     using System.Web.UI.HtmlControls;    
  10.     using System.Net;    
  11.     using System.Text;    
  12.     using System.IO;    
  13.        
  14.     namespace ToHtml    
  15.     {    
  16.         //51aspx.com生成静态页演示文件,转载请保留该信息    
  17.         public partial class _Default : System.Web.UI.Page    
  18.         {    
  19.             protected void Page_Load(object sender, EventArgs e)    
  20.             {    
  21.                    
  22.             }    
  23.        
  24.             //根据模板生成,保持在html文件夹中(部分源码搜集于网络)    
  25.             protected void Button1_Click(object sender, EventArgs e)    
  26.             {    
  27.                 //源码是替换掉模板中的特征字符    
  28.        
  29.                 string mbPath =Server.MapPath("template.htm");    
  30.                 Encoding code = Encoding.GetEncoding("gb2312");    
  31.                 StreamReader sr = null;    
  32.                 StreamWriter sw = null;    
  33.                 string str = null;    
  34.        
  35.                 //读取    
  36.                 try   
  37.                 {    
  38.                     sr = new StreamReader(mbPath, code);    
  39.                     str = sr.ReadToEnd();    
  40.        
  41.                 }    
  42.                 catch (Exception ex)    
  43.                 {    
  44.                     throw ex;    
  45.                 }    
  46.                 finally   
  47.                 {    
  48.                     sr.Close();    
  49.                 }    
  50.        
  51.                 //根据时间自动重命名,扩展名也可以自行修改    
  52.                 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";    
  53.                 str = str.Replace("$title$", txtTitle.Text);//替换Title    
  54.                 str = str.Replace("$content$", txtContent.Text);//替换content    
  55.        
  56.                 //生成静态文件    
  57.                 try   
  58.                 {    
  59.                     sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);    
  60.                     sw.Write(str);    
  61.                     sw.Flush();    
  62.        
  63.                 }    
  64.                 catch (Exception ex)    
  65.                 {    
  66.                     throw ex;    
  67.                 }    
  68.                 finally   
  69.                 {    
  70.                     sw.Close();    
  71.                     Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成,保存在htm文件夹下!");    
  72.                 }    
  73.        
  74.        
  75.             }    
  76.        
  77.        
  78.             //根据Url地址生成静态页保持    
  79.             protected void Button2_Click(object sender, EventArgs e)    
  80.             {    
  81.                 Encoding code = Encoding.GetEncoding("utf-8");    
  82.                 StreamReader sr = null;    
  83.                 StreamWriter sw = null;    
  84.                 string str = null;    
  85.        
  86.                 //读取远程路径    
  87.                 WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());    
  88.                 WebResponse myTemp = temp.GetResponse();    
  89.                 sr = new StreamReader(myTemp.GetResponseStream(), code);    
  90.                 //读取    
  91.                 try   
  92.                 {    
  93.                     sr = new StreamReader(myTemp.GetResponseStream(), code);    
  94.                     str = sr.ReadToEnd();    
  95.        
  96.                 }    
  97.                 catch (Exception ex)    
  98.                 {    
  99.                     throw ex;    
  100.                 }    
  101.                 finally   
  102.                 {    
  103.                     sr.Close();    
  104.                 }    
  105.                 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";    
  106.        
  107.                 //写入    
  108.                 try   
  109.                 {    
  110.                     sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);    
  111.                     sw.Write(str);    
  112.                     sw.Flush();    
  113.        
  114.                 }    
  115.                 catch (Exception ex)    
  116.                 {    
  117.                     throw ex;    
  118.                 }    
  119.                 finally   
  120.                 {    
  121.                     sw.Close();    
  122.                     Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");    
  123.                 }    
  124.        
  125.             }    
  126.         }    
  127.     }  

本文转载于http://www.itjianghu.net/120128/40936946840277890.htm