< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>
由于搜尋引擎對aspx頁面收錄和html頁面收錄率的差别以及頁面資源占用問題,我們很多時候需要實作ASPX頁面動态轉靜态。網上也有很多人
讨論其實作方法,本人實踐後總結兩種主流方法如下:
第一種方法:
使用模闆轉換,步驟如下:
1、建立MyConvert.cs類檔案
using System;
//記得添加以下三引用
using System.Text;
using System.Web;
using System.IO;
namespace TesConvert
{
/// <summary>
/// MyConvert 的摘要說明。
/// </summary>
public class MyConvert
public MyConvert()
//
// TODO: 在此處添加構造函數邏輯
}
public bool WriteFile(string strText,string strContent,string strAuthor)
string path = HttpContext.Current.Server.MapPath("/TesConvert/news/");//定義html檔案存放路徑
Encoding code = Encoding.GetEncoding("gb2312");//定義文字編碼
// 讀取模闆檔案
string temp = HttpContext.Current.Server.MapPath("/TesConvert/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 讀取檔案
catch(Exception exp)
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
string htmlfilename=path + DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替換内容
// 這時,模闆檔案已經讀入到名稱為str的變量中了
str = str.Replace("ShowArticle",strText); //模闆頁中的ShowArticle
str = str.Replace("title",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫檔案
sw = new StreamWriter(htmlfilename,false,code);
sw.Write(str);
sw.Flush();
catch(Exception ex)
HttpContext.Current.Response.Write(ex.Message);
finally
sw.Close();
return true;
2、TestNews.aspx檔案:
添加三和TextBox分别為:tbx_Title、tbx_Content、tbx_Author和一個Button:btn_AddNews。
TestNews.aspx.cs檔案
private void btn_AddNews_Click(object sender, System.EventArgs e)
MyConvert Hover = new MyConvert();
if(Hover.WriteFile(this.txb_Title.Text.ToString(),Server.HtmlDecode(this.txb_Content.Value),this.txb_Author.Text.ToString()))
Response.Write("添加成功");
else
Response.Write("生成HTML出錯!");
3、添加模闆text.html檔案
<head>ShowArticle</head>
<body>
title<br/>
content<br/>
author
</body>
說明:一.news檔案夾必須賦予asp.net使用者寫入的權限。這是一個簡單的實作例子,實際項目必須先将資料儲存到資料庫下面,在datagird中
調用資料庫下面html檔案的URL位址。二.預設情況下,我們是不能向TextBox、TextArea中添加html文法的,必須修改config檔案,在
<system.web>下面添加<pages validateRequest="false" />,但是這樣做的話,整個項目中都允許鍵入html标簽了,暫時還不知道其他的方。
缺點:這種方法是在ASP.net在頁面所有内容生成後、輸出内容前對頁面内容進行操作以前曾說過用HttpModule來在Response前更改,不夠靈活
,每行修改response,比較費力。
第二種方法:
重寫AttributeCollection.Render,比較靈活(msdn如是說:“在呈現階段,所有 ASP.NET 移動裝置擴充卡都通過一個稱為文本編寫器的對象
來編寫它們的輸出。文本編寫器對象是從 TextWriter 基類建立的。”)
可以寫個基類,如:
public class BasePage: System.Web.UI.Page
public BasePage()
protected override void Render(System.Web.UI.HtmlTextWriter writer)
string name=Request.Url.AbsolutePath.Substring(1,Request.Url.AbsolutePath.Length-1).Replace("aspx","htm");
string newurl="";
if(name.IndexOf("/")>0)
newurl=Server.MapPath("../") + name;
newurl=Server.MapPath("./") + name;
MemoryStream ms = new MemoryStream();
StreamWriter sww = new StreamWriter(ms);
StreamWriter swr = new StreamWriter(newurl);
System.Web.UI.HtmlTextWriter htmlw = new HtmlTextWriter(swr);
base.Render(htmlw);
htmlw.Flush();
htmlw.Close();
string strLL = System.Text.Encoding.UTF8.GetString(ms.ToArray());
Response.Write(strLL);
Response.Redirect(Request.Url.AbsoluteUri.Replace("aspx","htm"), true);
然後在需要生成靜态頁面的頁面中繼承就可以了。
說明:這種辦法是在Asp.net的生成動作完成之後,再進行一次轉換。
缺點:覺得本質上應該還是屬于頻繁post的aspx頁面。
<a href="http://www.cnblogs.com/tag/c%23%E4%B8%93%E6%A0%8F/feeds">#c#專欄</a>
本文轉自 netcorner 部落格園部落格,原文連結:http://www.cnblogs.com/netcorner/archive/2009/01/09/2912091.html,如需轉載請自行聯系原作者