自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

小議ASP.NET模板引擎技術(shù)的使用

開發(fā) 后端
我們將談?wù)摰氖茿SP.NET模板引擎技術(shù)的使用,希望本文能對(duì)大家跳出PHP模板引擎技術(shù),了解更多有所幫助。

我們將從PHP模板引擎技術(shù)談?wù)凙SP.NET模板引擎技術(shù),希望通過(guò)本文的實(shí)例和代碼,能讓大家在今后的開發(fā)過(guò)程中更加靈活的運(yùn)用ASP.NET模板引擎技術(shù)。

以前聽我朋友說(shuō)起php的模板引擎技術(shù)的時(shí)候似懂非懂哪時(shí)感覺(jué)真的很強(qiáng),一直在想asp.net有這種技術(shù)嗎?我不知道我的理解是不是對(duì)的.其實(shí)asp.net模板引擎技術(shù)就是先建好一個(gè)靜態(tài)的html頁(yè)面我們稱它為模板頁(yè),你如果有不同形式的頁(yè)面哪就得建立不同的靜態(tài)模板頁(yè),然后在后臺(tái)用文件操作往這個(gè)文件里寫東西然后在把這個(gè)模板頁(yè)另存到一個(gè)靜態(tài)頁(yè)面的目錄,不好意思可能我的理解太俗,如果有更好的理解和想法可以在apolov發(fā)文章告訴我謝謝。現(xiàn)在我附加一下代碼

Default.aspx這個(gè)頁(yè)面只有幾個(gè)textbox控件和兩個(gè)按妞控件

  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生成靜態(tài)頁(yè)</title> 
  6. </head> 
  7. <body> 
  8.     <form id="form1" runat="server"> 
  9.     <div> 
  10.         標(biāo)題:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br /> 
  11.         內(nèi)容:<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="根據(jù)模板生成" /><br /> 
  15.         <br /> 
  16.         <br /> 
  17.         Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="請(qǐng)確認(rèn)Url地址的存在" Width="359px"></asp:TextBox> 
  18.         <br /> 
  19.         <br /> 
  20.         <asp:Button ID="Button2" runat="server" Text="根據(jù)Url地址生成" OnClick="Button2_Click" /></div> 
  21.     </form> 
  22. </body> 
  23. </html> 

要準(zhǔn)備的模板頁(yè)代碼,htm文件頁(yè)面比較簡(jiǎn)單,如果有興趣的朋友可以做成更復(fù)雜的模板頁(yè)嘿嘿

  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$ 生成靜態(tài)頁(yè)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.     內(nèi)容:$content$ </td> 
  25.   </tr> 
  26. </table> 
  27.  
  28. </body> 
  29. </html> 

后臺(tái)生成靜態(tài)頁(yè)面的代碼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生成靜態(tài)頁(yè)演示文件,轉(zhuǎn)載請(qǐng)保留該信息  
  17.     public partial class _Default : System.Web.UI.Page  
  18.     {  
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.              
  22.         }  
  23.  
  24.         //根據(jù)模板生成,保持在html文件夾中(部分源碼搜集于網(wǎng)絡(luò))  
  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.             //根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改  
  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.             //生成靜態(tài)文件  
  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>已經(jīng)生成,保存在htm文件夾下!");  
  72.             }  
  73.  
  74.  
  75.         }  
  76.  
  77.  
  78.         //根據(jù)Url地址生成靜態(tài)頁(yè)保持  
  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.             //讀取遠(yuǎn)程路徑  
  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>已經(jīng)生成,保存在htm文件夾下!");  
  123.             }  
  124.  
  125.         }  
  126.     }  

原文標(biāo)題:Asp.net模板引擎技術(shù)

鏈接:http://www.cnblogs.com/resoar/archive/2009/10/09/1579370.html

【編輯推薦】

  1. ASP.NET MVC單元測(cè)試:HttpContext類的Path屬性解惑
  2. 自定義的ControllerFactory:接口實(shí)現(xiàn),支持Area
  3. ASP.NET Routing之“解析URL”功能詳解
  4. 為ASP.NET MVC應(yīng)用添加自定義路由
  5. 學(xué)習(xí)ASP.NET MVC路由的使用方法
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2011-01-19 11:17:36

2009-08-25 15:18:58

ASP.NET數(shù)據(jù)We

2012-05-09 13:29:42

ASP.NET

2009-07-21 15:11:14

ASP.NET Rou

2012-05-04 15:54:16

ASP.NET

2009-08-07 15:32:28

ASP.NET復(fù)合控件

2009-08-05 14:27:55

ASP.NET模板基本

2009-08-07 15:24:16

ASP.NET模板控件

2014-06-30 15:10:32

2009-04-07 16:21:35

ASP.NETAJAXWCF

2009-07-28 16:42:02

ViewState技術(shù)ASP.NET應(yīng)用

2009-07-27 16:30:49

母版資源ASP.NET網(wǎng)頁(yè)模板

2012-06-26 10:24:51

Asp.Net框架Razor

2009-07-24 15:35:00

ASP.NET Gri

2009-07-29 14:12:45

ASP.NET tra

2009-07-24 13:08:40

AJAX技術(shù)ASP.NET

2009-07-29 15:38:01

2009-08-03 10:07:20

ASP.NET Ses

2009-08-10 14:55:43

ASP.NET htt

2009-07-23 15:44:39

ASP.NET MVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)