重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
本篇文章為大家展示了ASP.NET中怎么使用模板引擎技術,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
十載的六枝網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網營銷推廣的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整六枝建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。創新互聯公司從事“六枝網站設計”,“六枝網站推廣”以來,每個客戶項目都認真落實執行。
Default.aspx這個頁面只有幾個textbox控件和兩個按妞控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" ValidateRequest="false" Inherits="ToHtml._Default" %>Asp.net生成靜態頁
要準備的模板頁代碼,htm文件頁面比較簡單,如果有興趣的朋友可以做成更復雜的模板頁嘿嘿
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">$title$ 生成靜態頁title>
$title$ |
內容:$content$ |
后臺生成靜態頁面的代碼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生成靜態頁演示文件,轉載請保留該信息 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //根據模板生成,保持在html文件夾中(部分源碼搜集于網絡) 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(); } //根據時間自動重命名,擴展名也可以自行修改 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; str = str.Replace("$title$", txtTitle.Text);//替換Title str = str.Replace("$content$", txtContent.Text);//替換content //生成靜態文件 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("恭喜"+fileName+"已經生成,保存在htm文件夾下!"); } } //根據Url地址生成靜態頁保持 protected void Button2_Click(object sender, EventArgs e) { Encoding code = Encoding.GetEncoding("utf-8"); StreamReader sr = null; StreamWriter sw = null; string str = null; //讀取遠程路徑 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("恭喜" + fileName + "已經生成,保存在htm文件夾下!"); } } } }
上述內容就是ASP.NET中怎么使用模板引擎技術,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。