我们将从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.     </body>   
  28.     </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.     namespace ToHtml    
  14.     {    
  15.         //51aspx.com生成静态页演示文件,转载请保留该信息    
  16.         public partial class _Default : System.Web.UI.Page    
  17.         {    
  18.             protected void Page_Load(object sender, EventArgs e)    
  19.             {    

  20.             }    

  21.             //根据模板生成,保持在html文件夹中(部分源码搜集于网络)    
  22.             protected void Button1_Click(object sender, EventArgs e)    
  23.             {    
  24.                 //源码是替换掉模板中的特征字符    

  25.                 string mbPath =Server.MapPath("template.htm");    
  26.                 Encoding code = Encoding.GetEncoding("gb2312");    
  27.                 StreamReader sr = null;    
  28.                 StreamWriter sw = null;    
  29.                 string str = null;    

  30.                 //读取    
  31.                 try   
  32.                 {    
  33.                     sr = new StreamReader(mbPath, code);    
  34.                     str = sr.ReadToEnd();    

  35.                 }    
  36.                 catch (Exception ex)    
  37.                 {    
  38.                     throw ex;    
  39.                 }    
  40.                 finally   
  41.                 {    
  42.                     sr.Close();    
  43.                 }    

  44.                 //根据时间自动重命名,扩展名也可以自行修改    
  45.                 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";    
  46.                 str = str.Replace("$title$", txtTitle.Text);//替换Title    
  47.                 str = str.Replace("$content$", txtContent.Text);//替换content    

  48.                 //生成静态文件    
  49.                 try   
  50.                 {    
  51.                     sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);    
  52.                     sw.Write(str);    
  53.                     sw.Flush();    

  54.                 }    
  55.                 catch (Exception ex)    
  56.                 {    
  57.                     throw ex;    
  58.                 }    
  59.                 finally   
  60.                 {    
  61.                     sw.Close();    
  62.                     Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成,保存在htm文件夹下!");    
  63.                 }    


  64.             }    


  65.             //根据Url地址生成静态页保持    
  66.             protected void Button2_Click(object sender, EventArgs e)    
  67.             {    
  68.                 Encoding code = Encoding.GetEncoding("utf-8");    
  69.                 StreamReader sr = null;    
  70.                 StreamWriter sw = null;    
  71.                 string str = null;    

  72.                 //读取远程路径    
  73.                 WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());    
  74.                 WebResponse myTemp = temp.GetResponse();    
  75.                 sr = new StreamReader(myTemp.GetResponseStream(), code);    
  76.                 //读取    
  77.                 try   
  78.                 {    
  79.                     sr = new StreamReader(myTemp.GetResponseStream(), code);    
  80.                     str = sr.ReadToEnd();    

  81.                 }    
  82.                 catch (Exception ex)    
  83.                 {    
  84.                     throw ex;    
  85.                 }    
  86.                 finally   
  87.                 {    
  88.                     sr.Close();    
  89.                 }    
  90.                 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";    

  91.                 //写入    
  92.                 try   
  93.                 {    
  94.                     sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);    
  95.                     sw.Write(str);    
  96.                     sw.Flush();    

  97.                 }    
  98.                 catch (Exception ex)    
  99.                 {    
  100.                     throw ex;    
  101.                 }    
  102.                 finally   
  103.                 {    
  104.                     sw.Close();    
  105.                     Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");    
  106.                 }    

  107.             }    
  108.         }    
  109.     }  

本文转载于​