Asp.net 產生靜態頁面

來源:互聯網
上載者:User

標籤:blog   http   os   io   檔案   for   art   ar   

      第一次發表,有什麼錯誤,請大家諒解噢!

      如果不明白的話,建議自己拷一次。 就會的了。。

    開發步驟:

      1、路徑映射類(UrlMapping),主要對路徑進行拆分、拼接。(關鍵的一步)

      2、過濾流類(FilterStream),主要負責產生靜態頁面。

      3、靜態頁面類(HtmlPage),主要是調用UrlMapping和FilterStream類,

          哪個頁面想靜態化,就繼承這個類。

      4、HtmlHandler類,路徑尾碼為Html的,都由它來處理,與HtmlPage類相似。

      5、HtmlPanel類(控制項),頁面帶上這個控制項,超連結會靜態化。(詳情請下載源碼包)

      部分代碼:

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Eshop.Web.UI {     /// <summary>     /// 路徑映射     /// </summary>     public static class UrlMapping     {         //Aspx 轉換到 Html         public static string AspxToHtml(string url)         {             //判斷路徑是否為空白             if (string.IsNullOrEmpty(url))             {                 throw new ArgumentNullException("路徑不可為空");             }             //分割路徑             string[] temp = url.Split(‘?‘);             if (temp.Length != 1 && temp.Length != 2)             {                 throw new ArgumentException(String.Format("路徑 {0} 及其參數錯誤", url));             }             //擷取路徑尾碼             string ext = Path.GetExtension(temp[0]);                if (!(ext.Equals(".aspx", StringComparison.OrdinalIgnoreCase)))             {                 throw new ArgumentException(String.Format("路徑 {0} 類型必須為ASPX", url));             }             //截取.aspx中前面的內容             int offset = temp[0].LastIndexOf(‘.‘);             string resource = temp[0].Substring(0, offset);             //路徑不帶參數時             if (temp.Length == 1 || string.IsNullOrEmpty(temp[1]))             {                 return string.Format("{0}.html", resource);    //拼接             }             //路徑帶參數時             return string.Format("{0}___{1}.html", resource, temp[1]); //拼接         }                 //Html 轉換到 Aspx         public static string HtmlToAspx(string url)         {             //判斷路徑是否為空白             if (string.IsNullOrEmpty(url))             {                 throw new ArgumentNullException("路徑不可為空");             }             string ext = Path.GetExtension(url);             if (!(ext.Equals(".html", StringComparison.OrdinalIgnoreCase)))             {                 throw new ArgumentException(String.Format("路徑 {0} 類型必須為HTML", url));             }             string[] temp = url.Split(new String[] { "___", "." }, StringSplitOptions.RemoveEmptyEntries);             if (temp.Length == 2)             {                 return string.Format("{0}.aspx", temp[0]);             }             if (temp.Length == 3)             {                 return String.Format("{0}.aspx?{1}", temp[0], temp[1]);             }             throw new ArgumentException(String.Format("資源 {0} 及其參數錯誤", url));         }     } } 

  

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Eshop.Web.UI {     /// <summary>     /// 靜態網頁儲存     /// </summary>     public class FilterStream : Stream     {         private Stream respStream = null;         private Stream fileStream = null;         public FilterStream(Stream respStream, string filePath)         {             if (respStream == null)                 throw new ArgumentNullException("輸出資料流不可為空");             this.respStream = respStream;                         try             {                 this.fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);  //寫入到檔案夾中             }             catch { }         }         public override bool CanRead         {             get { return this.respStream.CanRead; }         }         public override bool CanSeek         {             get { return this.respStream.CanSeek; }         }         public override bool CanWrite         {             get { return this.respStream.CanWrite; }         }         public override void Flush()         {             this.respStream.Flush();             if (this.fileStream != null)             {                 this.fileStream.Flush();             }         }         public override long Length         {             get { return this.respStream.Length; }         }         public override long Position         {             get             {                 return this.respStream.Position;             }             set             {                 this.respStream.Position = value;                 if (this.fileStream != null)                 {                     this.fileStream.Position = value;                 }             }         }         public override int Read(byte[] buffer, int offset, int count)         {             return this.respStream.Read(buffer, offset, count);         }         public override long Seek(long offset, SeekOrigin origin)         {             if (this.fileStream != null)             {                 this.fileStream.Seek(offset, origin);             }             return this.respStream.Seek(offset, origin);         }         public override void SetLength(long value)         {             this.respStream.SetLength(value);             if (this.fileStream != null)             {                 this.fileStream.SetLength(value);             }         }         public override void Write(byte[] buffer, int offset, int count)         {             this.respStream.Write(buffer, offset, count);             if (this.fileStream != null)             {                 this.fileStream.Write(buffer, offset, count);             }         }         protected override void Dispose(bool disposing)         {             base.Dispose(disposing);             this.respStream.Dispose();             if (this.fileStream != null)             {                 this.fileStream.Dispose();             }         }     } } 

  

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Eshop.Web.UI {     /// <summary>     /// 哪個頁面想靜態化,就繼承這個類     /// </summary>     public class HtmlPage:Page     {         // <summary>         /// 擷取實體路徑,判斷檔案夾中有沒有存在這個檔案         /// 不存在的話,就會調用FilterStream類進行建立,並寫入內容         /// 存在的話,就直接顯示頁面         /// </summary>         public override void ProcessRequest(HttpContext context)         {             HttpRequest req = context.Request;             HttpResponse resp = context.Response;             string htmlPage = UrlMapping.AspxToHtml(req.RawUrl);             string htmlFile = context.Server.MapPath(htmlPage);             if (File.Exists(htmlFile))             {                 resp.Redirect(htmlPage);                 return;             }             // Html 頁面不存在             resp.Filter = new FilterStream(resp.Filter, htmlFile);             base.ProcessRequest(context);         }     } } 

  

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; namespace Eshop.Web.UI  {     /// <summary>     /// 尾碼為HTML的,都經這裡處理     /// web.config     /// <remove verb="*" path="*.HTML"/>     /// <add verb="*" path="*.HTML" type="Eshop.Web.UI.HtmlHandler,AspxToHtmlDemo"/>     /// </summary>     public class HtmlHandler:IHttpHandler     {         public bool IsReusable         {             get { return false; }         }         /// <summary>         /// 擷取實體路徑,判斷檔案夾中有沒有存在這個檔案         /// 不存在的話,就會調用FilterStream類進行建立,並寫入內容         /// 存在的話,就直接顯示頁面         /// </summary>         public void ProcessRequest(HttpContext context)         {             HttpRequest request = context.Request;             HttpResponse response = context.Response;             string htmlPage = request.RawUrl;             string htmlFile = context.Server.MapPath(htmlPage);             if (File.Exists(htmlFile))             {                 response.WriteFile(htmlFile);                 return;             }             //Html 檔案不存在             string aspxPage = UrlMapping.HtmlToAspx(htmlPage);             response.Redirect(aspxPage);         }     } } 

  

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Eshop.Web.Index" %> <!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>AspxToHtml Demo</title> </head> <body>     <form id="form1" runat="server">     <div>         <h1>AspxToHtml Demo</h1>         <br />                 <html:HtmlPanel ID="hp" runat="server">             <asp:HyperLink ID="Hy" runat="server" NavigateUrl="~/Index.aspx?page=2">                    點擊             </asp:HyperLink>             <br />             <a href="~/Index.aspx?page=2" runat="server">Hello</a>         </html:HtmlPanel>     </div>     </form> </body> </html>

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.