動態網頁面靜態化、、

來源:互聯網
上載者:User

1、拆中做法、 、 判斷靜態檔案是否存在、不存在就建立一個(提前編輯好一個靜態頁模板

<!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>
</head>
<body>
    <cebter>
    <fieldset>
        <legend>詳細資料</legend>
        <table>
            <tr><td>書名:</td><td>[$title]</td></tr>
            <tr><td>作者:</td><td>[$author]</td></tr>
            <tr><td>出版社:</td><td>[$publisher]</td></tr>
        </table>
    </fieldset>
</cebter>
</body>
</html>
、最後根據預留位置得到靜態頁面、 、 、 、)。

存在就直接轉向靜態檔案、 、

2、配置web.config檔案、

  <httpHandlers>
            <add verb="*" type="HtmlHandler,App_Code" path="*.html"/>
        </httpHandlers>

3、實現 IHttphandler 類、 、

  public void ProcessRequest(HttpContext context)
    {
        context.Request.ContentEncoding = Encoding.GetEncoding("utf-8");         // 編碼格式 、 、
        context.Response.ContentEncoding = Encoding.GetEncoding("utf-8");   
        string url = context.Request.RawUrl;                                                               // 獲得請求的原始 url 、、
        string temp = url.Remove(url.Length - 5);
        StringBuilder sb = new StringBuilder();
        foreach (char c in temp.Skip(temp.LastIndexOf('_')+1))
        {
            sb.Append(c.ToString());
        }                                                                                                                       //  取出 id  、、
        //context.Response.Write(sb);
        string urlPath = "~/htmls/book" + sb;
        lock(context.Application)                                                                           // 應用程式一次只能訪問一個。 。
        {
            if (!File.Exists(context.Server.MapPath(urlPath)))
            {
                //如果不存在當前路徑對應的檔案則組建檔案
                string strHtml = string.Empty;
                using (StreamReader sr = new StreamReader(context.Server.MapPath("~/htmls/Template.htm")))
                {
                    strHtml = sr.ReadToEnd();
                }
                strHtml = strHtml.Replace("[$title]", "C#進階編程");
                strHtml = strHtml.Replace("[$author]", "張小三");
                strHtml = strHtml.Replace("[$publisher]", "華爾街金融出版社");
                using(StreamWriter sw=new StreamWriter(context.Server.MapPath(urlPath))){
                    sw.Write(strHtml);
                }
            }

        }
        context.Server.Execute(urlPath);
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.