Handlers 頁面靜態化

頁面靜態化介紹

瀏覽頁面時 我們會發現凡是訪問量稍大的網站,他們的很多頁面都是靜態頁面的地址。
是否說明這些網站都不傾向於使用動態頁面的技術呢?
        當然不是  只是這些網站使用了頁面靜態化技術


        頁面靜態化好處

        1.有利於搜索引擎搜索,搜索引擎在網絡上獲取頁面時,是優先獲取html靜態頁面的
        2.頁面靜態化技術也是使用後臺處理的,因此,他並沒有失去動態頁面易於維護的優勢
        3.有利於網頁的性能呵呵安全
        
        頁面靜態化方法
        1.僞靜態 顧名思義 並沒有將頁面轉化爲靜態頁面,而是通過URL重寫技術,將頁面路徑重
寫爲.html路徑
        
        2.真靜態,這種方案真正將動態頁面批量轉化爲靜態頁面,並儲存在服務器上,
        這樣用戶訪問靜態頁面時,該頁面真是存在,不需要重定向,但自方法需要提前轉化
        
        3.折中法  這種方案是前面兩種方案的這種做法,也是使用普遍的做法。當用戶請求摸個靜態
頁面時,先判定該靜態頁面是否存在,如果存在則直接找到頁面返回給客戶端, 如果不存在,則執行
轉化操作生成靜態頁面,將其發送給客戶端並保存在服務器上,下次
訪問,則無需在執行轉化操作
        
        

 折中法

折中法處理 思路

            1.獲取當前請求的URl 截取其中ID

        2.根據汽車ID 拼接靜態頁面 判定頁面是否存在

        3.如果頁面不存在  查詢汽車信息,替換模版標籤,生成靜態頁面

        4.此時靜態頁面已存在  直接找到靜態頁面,將其發送至客戶端

        6.修改文件配置

        7.測試


代碼實例

namespace ch2
{
    public class CarHd : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            //獲取請求的原始URl
            string strurl = context.Request.RawUrl;
            //最後斜槓的位子
            int last = strurl.LastIndexOf("_");
            //最後點的位置
            int dot = strurl.LastIndexOf(".");
            //截取信息
            int name = int.Parse(strurl.Substring(last + 1, dot - last - 1));
            //當前訪問的汽車詳情頁面物理路徑
            string carPath = context.Server.MapPath("info_" + name + ".html");
            //判定當前對應的靜態頁面是否存在
            if (!File.Exists(carPath)) 
            {
                //初始化數據
                CarList carlist = new CarList();
                //靜態頁面模板路徑
                string templatePath = context.Server.MapPath("T5.html");
                //讀取模板
                string Temp = ReadTemplate(templatePath);
                //替換模板中的標籤
                Temp = Temp.Replace("{$Name}", carlist.list[name].Name);
                Temp = Temp.Replace("{$Brand}", carlist.list[name].Brand);
                //保存靜態頁面
                WriteHTML(carPath, Temp);
            }
            //將靜態頁面發送至客戶端
            context.Response.WriteFile(carPath);
        }

        //讀取模板的方法、
        private string ReadTemplate(string path) {
            //檢測文件是否存在
            if (!File.Exists(path))
            {
                //模板文件不存在拋出異常
                throw new Exception("錯誤101");          
            }
            //創建文件流
            FileStream fs = new FileStream(path, FileMode.Open);
            //創建讀取器
            StreamReader sr = new StreamReader(fs);
            //讀取文件流中文本
            string html = sr.ReadToEnd();
            //關閉讀取器
            sr.Close();
            //關閉文件流
            fs.Close();
            //返回讀取的模版html
            return html;
        }

        //編寫靜態文件
        private void WriteHTML(string path,string html)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(html);
            sw.Close();
            fs.Close();
        }
    }
}

namespace ch2
{
    public class Car
    {
        public string  Name { get; set; }

        public string Brand { get; set; }

    }


    public class CarList
    {
        public List<Car> list = new List<Car>() {
            new Car() { Name="1",Brand="1"},
            new Car() { Name="5",Brand="5"},
            new Car() { Name="2",Brand="2"},
            new Car() { Name="3",Brand="3"},
            new Car() { Name="4",Brand="4"},

        };
    }
}

//頁面
<form id="form1" runat="server">
         <a href="car/info_1.html">1</a>
         <a href="car/info_2.html">2</a>
    </form>




發佈了44 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章