一說到新聞系統的話,一定會談到靜態頁面生成的,因為靜態頁面不但是讀取速度快,而且又安全;
靜態頁面的生成不管是小到現在的企業網站大至網易,qq等門戶都用到了;
那么我們如何來生成靜態頁呢?
以什么方式生成靜態頁面呢……
在生成靜態頁面的時候有那些是要注意的呢:
靜態頁面命名 
統一存放目錄 
靜態頁面模板 
頁面生成
一般來說,在原來新聞系統的基礎上我們可以根據get此頁面請求的內容再生成(比如:http;//www.test.com/news.aspx?id=1,get此頁面代碼直接寫至一個文本文件并以html命名即可);
在這里我所采用的是模板生成,先用dw做一個網頁模板,將標題,內容等將要動態實現的內容先以$title$等替換,等在生成的時候替換成新聞的內容;
命名:在生成的時候一般來說是采用新聞發布的時間轉換成的字符串,這個是不會重復的
另外我還按年份月份把這些靜態文件存放在不同的目錄,以便于管理,
在這里根據一個新聞的id調用方法writenews()給定參數id,它就會根據此id從數據庫中讀取內容,再根據靜態模板頁面html/test.html生成新的靜態頁面存放在相應年份月份的目錄
好了,下面是代碼:
using system;
using system.io;
using system.web;
using system.text;
namespace powerleader.components
...{
    /**//// <summary>
    /// writetohtml 的摘要說明。
    /// </summary>
    public class writetohtml
    ...{
        public writetohtml()
        ...{
            //
            // todo: 在此處添加構造函數邏輯
            //
        }
        public static void writenews(int id)
        ...{
            news news = new news();            
            news.newsdetails newsdetails = new powerleader.components.news.newsdetails();
            newsdetails = news.getnews(id);
            bool flag;
            flag = writefile(newsdetails);
        }
        public static bool writefile(news.newsdetails newsdetails) 
        ...{
            directory.createdirectory(httpcontext.current.server.mappath("/powerleader/html/"+newsdetails.addtime.tostring("yyyy")+"/"+newsdetails.addtime.tostring("mm")));
            string path = httpcontext.current.server.mappath("../html/"+newsdetails.addtime.tostring("yyyy")+"/"+newsdetails.addtime.tostring("mm")+"/");
            encoding code = encoding.getencoding("gb2312");
            // 讀取模板文件
            string temp = httpcontext.current.server.mappath("../html/text.html");
            streamreader sr = null;
            streamwriter sw = null;
            string stringtempcode = "";  
            try
            ...{
                sr = new streamreader(temp, code);
                stringtempcode = sr.readtoend(); // 讀取文件
            }
            catch(exception exp)
            ...{
                httpcontext.current.response.write(exp.message);
                httpcontext.current.response.end();
                sr.close();
            }   
            string htmlfilename = newsdetails.addtime.tostring("yyyymmddhhmmss") + ".html";
            // 替換內容
            // 這時,模板文件已經讀入到名稱為str的變量中了
            stringtempcode = stringtempcode.replace("$pagetitle$","抗戰online官方網站...");
            stringtempcode = stringtempcode.replace("$type$",newsdetails.type.tostring().trim());
            stringtempcode = stringtempcode.replace("$author$",newsdetails.author.tostring().trim());
            stringtempcode = stringtempcode.replace("$from$",newsdetails.from.trim());
            stringtempcode = stringtempcode.replace("$time$",newsdetails.addtime.tostring().trim());
            stringtempcode = stringtempcode.replace("$title$",newsdetails.title.trim());
            stringtempcode = stringtempcode.replace("$content$",newsdetails.content);
            // 寫文件
            try
            ...{
                sw = new streamwriter(path + htmlfilename , false, code);
                sw.write(stringtempcode);
                sw.flush();
            }
            catch(exception ex)
            ...{
                httpcontext.current.response.write(ex.message);
                httpcontext.current.response.end();
            }
            finally
            ...{
                sw.close();
            }
            return true;
        }
    }
}
新聞熱點
疑難解答
圖片精選