小議ASP.NET模板引擎技術(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è)按妞控件
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" ValidateRequest="false" Inherits="ToHtml._Default" %>
- <!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 runat="server">
- <title>Asp.net生成靜態(tài)頁(yè)</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 標(biāo)題:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />
- 內(nèi)容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"
- Width="350px"></asp:TextBox><br />
- <br />
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="根據(jù)模板生成" /><br />
- <br />
- <br />
- Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="請(qǐng)確認(rèn)Url地址的存在" Width="359px"></asp:TextBox>
- <br />
- <br />
- <asp:Button ID="Button2" runat="server" Text="根據(jù)Url地址生成" OnClick="Button2_Click" /></div>
- </form>
- </body>
- </html>
要準(zhǔn)備的模板頁(yè)代碼,htm文件頁(yè)面比較簡(jiǎn)單,如果有興趣的朋友可以做成更復(fù)雜的模板頁(yè)嘿嘿
- !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$ 生成靜態(tài)頁(yè)title>
- <style type="text/css">
- <!--
- .STYLE1 {
- font-size: 16px;
- font-weight: bold;
- }
- -->
- </style>
- </head>
- <body>
- <br />
- <br />
- <table width="100%" border="0" bgcolor="#339900">
- <tr>
- <td height="34" align="center" bgcolor="#FFFFFF"><span class="STYLE1">$title$ </span></td>
- </tr>
- <tr>
- <td height="42" bgcolor="#FFFFFF"><br />
- <br />
- 內(nèi)容:$content$ </td>
- </tr>
- </table>
- </body>
- </html>
后臺(tái)生成靜態(tài)頁(yè)面的代碼Default.aspx.cs主要用到了文件操做
- sing System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Net;
- using System.Text;
- using System.IO;
- namespace ToHtml
- {
- //51aspx.com生成靜態(tài)頁(yè)演示文件,轉(zhuǎn)載請(qǐng)保留該信息
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //根據(jù)模板生成,保持在html文件夾中(部分源碼搜集于網(wǎng)絡(luò))
- protected void Button1_Click(object sender, EventArgs e)
- {
- //源碼是替換掉模板中的特征字符
- string mbPath =Server.MapPath("template.htm");
- Encoding code = Encoding.GetEncoding("gb2312");
- StreamReader sr = null;
- StreamWriter sw = null;
- string str = null;
- //讀取
- try
- {
- sr = new StreamReader(mbPath, code);
- str = sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sr.Close();
- }
- //根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改
- string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
- str = str.Replace("$title$", txtTitle.Text);//替換Title
- str = str.Replace("$content$", txtContent.Text);//替換content
- //生成靜態(tài)文件
- try
- {
- sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
- sw.Write(str);
- sw.Flush();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sw.Close();
- Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已經(jīng)生成,保存在htm文件夾下!");
- }
- }
- //根據(jù)Url地址生成靜態(tài)頁(yè)保持
- protected void Button2_Click(object sender, EventArgs e)
- {
- Encoding code = Encoding.GetEncoding("utf-8");
- StreamReader sr = null;
- StreamWriter sw = null;
- string str = null;
- //讀取遠(yuǎn)程路徑
- WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
- WebResponse myTemp = temp.GetResponse();
- sr = new StreamReader(myTemp.GetResponseStream(), code);
- //讀取
- try
- {
- sr = new StreamReader(myTemp.GetResponseStream(), code);
- str = sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sr.Close();
- }
- string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
- //寫入
- try
- {
- sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
- sw.Write(str);
- sw.Flush();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sw.Close();
- Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成,保存在htm文件夾下!");
- }
- }
- }
- }
原文標(biāo)題:Asp.net模板引擎技術(shù)
鏈接:http://www.cnblogs.com/resoar/archive/2009/10/09/1579370.html
【編輯推薦】