頁面靜態化分為兩種:偽靜態和真靜態,這里主要介紹的是真靜態。
進入正題之前先簡單介紹一下SSI和shtml:
1)、SSI是Server Side Include的簡稱(服務器端嵌入)
2)、shtml是包含有嵌入式服務器方包含命令的HTML文本。在被傳送給瀏覽器之前,服務器會對shtml文檔進行
完全的讀取、分析以及修改。
想要讓IIS支持服務器包含,還需要簡單配制一下,打開服務器端包含即可。啟用或關閉Windows功能->
Internet Information Services->萬維網服務->應用程序開發功能->服務器端包含。

然后打開IIS,在主頁上找到處理程序映射

你可以發現*.shtm和*.shtml的狀態是已啟用。

到這里,前期工作已經完成了。
來看看有那些是用了這個技術的網站:
1)、新浪網的新聞頁面

2)、鳳凰網的新聞頁面

實現這個主要就是主要就是生成xxx.shtml文件
接下來說一下個人的想法:要生成一個shtml后綴的文件,有兩方面考慮:路徑和內容。
路徑:{路徑名}/{文件名}.shtml
路徑名:隨你喜歡,只要最后你能獲取到這個路徑就行,我是放在項目的某個文件夾下面
文件名:也是個人喜好,可以是id,可以是其他,我是用的id
內容:往指定路徑的文件寫東西
可以分為3步:
1)、切分:將頁面切分成N個子模塊,什么頭啊,尾啊,導航欄啊。。。。練習的話,你想怎么分,分幾個,按自己的喜好來,
實際開發要用的話,你是頭就你說了算,不然就是要乖乖聽話。
2)、模板:寫一個頁面的總模板,把切分的子模塊按一定的順序排列好,在要放title的地方寫一個字符串在要放新聞內容的那一
塊寫一個字符串,把你覺得要動態放進頁面的東西,用自己熟悉的方式在頁面標志出來。就相當於mvc中的那個
@RenderBody差不多的功能,
3)、填坑:讀取總模板,把那些想動態加進去的坑填補好,就可以生成一個完整的頁面了。
實現如下:
切分:(我是隨便切的)


模板:(主要是用了include)
<!--#include file="/Template/head_start.html"-->
<title>@title</title>
<!--#include file="/Template/head_end.html"-->
<!--#include file="/Template/body_start.html"-->
<main class="container body-content">
<div style="text-align:center;"><span>@news_title</span></div>
@news_content
</main>
<!--#include file="/Template/body_end.html"-->
填坑:(操作IO)
/// <summary> /// 生成shtml頁面 /// </summary> /// <param name="newsEntity">新聞實體</param> /// <returns></returns> public static bool GenerateShtmlPage(News newsEntity) { //返回信息 string strMessage = string.Empty; //頁面模板完整路徑 string strTemplateFullPath = string.Format("{0}Template/{1}", AppDomain.CurrentDomain.BaseDirectory, "newstemplate.html"); //保存shtml頁面的絕對路徑 string strStaticPageAbsolutePath = GetStaticPageAbsolutePathByNewsId(newsEntity.NewsId); //獲取模板占位符數組 string[] arrPlaceholder = new string[3]; arrPlaceholder[0] = "@title"; arrPlaceholder[1] = "@news_title"; arrPlaceholder[2] = "@news_content"; //獲取填充到模板中的占位符所對應的數據數組 string[] arrReplaceContent = new string[3]; arrReplaceContent[0] = newsEntity.NewsTitle; arrReplaceContent[1] = newsEntity.NewsTitle; arrReplaceContent[2] = newsEntity.NewsContent; //生成shtml頁面 return GenerateShtmlPage(strStaticPageAbsolutePath, strTemplateFullPath, arrPlaceholder, arrReplaceContent, out strMessage); }
/// <summary> /// 根據靜態的HTML頁面模板生成靜態頁面 /// </summary> /// <param name="strStaticPageAbsolutePath">存放靜態頁面所在絕對路徑</param> /// <param name="strTemplateAbsolutePath">靜態模板頁面的絕對路徑</param> /// <param name="arrPlaceholder">占位符數組</param> /// <param name="arrReplaceContent">要替換的內容數組</param> /// <param name="strMessage">返回信息</param> /// <returns>生成成功返回true,失敗false</returns> public static bool GenerateStaticPage(string strStaticPageAbsolutePath, string strTemplateAbsolutePath, string[] arrPlaceholder, string[] arrReplaceContent, out string strMessage) { bool isSuccess = false; try { //生成存放靜態頁面目錄 if (!Directory.Exists(Path.GetDirectoryName(strStaticPageAbsolutePath))) { Directory.CreateDirectory(Path.GetDirectoryName(strStaticPageAbsolutePath)); } //驗證占位符 if (arrPlaceholder.Length != arrReplaceContent.Length) { strMessage = string.Format("生成靜態頁面失敗,占位符數組個數和替換內容個數不相等!存放路徑為:{0}", strStaticPageAbsolutePath); return false; } //生成存放靜態頁面文件 if (File.Exists(strStaticPageAbsolutePath)) { File.Delete(strStaticPageAbsolutePath); } //獲取模板HTML StringBuilder strHtml = new StringBuilder(); strHtml.Append(File.ReadAllText(strTemplateAbsolutePath, Encoding.UTF8)); //替換模板占位符,獲取要生成的靜態頁面HTML for (int i = 0; i < arrPlaceholder.Length; i++) { strHtml.Replace(arrPlaceholder[i], arrReplaceContent[i]); } //生成靜態頁面 File.WriteAllText(strStaticPageAbsolutePath, strHtml.ToString()); strMessage = string.Format("生成靜態頁面成功!存放路徑:{0}", strStaticPageAbsolutePath); isSuccess = true; } catch (IOException ex) { strMessage = ex.Message; isSuccess = false; } catch (Exception ex) { strMessage = ex.Message; isSuccess = false; } return isSuccess; }
/// <summary> /// 根據新聞id生成靜態頁面的絕對路徑 /// </summary> /// <param name="newsId">新聞id</param> /// <returns></returns> private static string GetShtmlPageAbsolutePathByNewsId(int newsId) { //靜態頁面名稱 string strShtmlPageName = string.Format("{0}.shtml", newsId); //靜態頁面相對路徑 string strShtmlPageRelativePath = string.Format("newspage\\{0}\\{1}", DateTime.Now.ToString("yyyy/MM/dd").Replace('/', '\\'), strShtmlPageName); //靜態頁面完整路徑 string strShtmlPageAbsolutePath = AppDomain.CurrentDomain.BaseDirectory + strShtmlPageRelativePath; return strShtmlPageAbsolutePath; }
到這里,已經生成了我們想要的xxx.shtml文件了。最后我們要做的就是訪問。
假設要訪問id為29的新聞,其url為:http:127.0.0.1/news/detials/29,生成的路徑為/newspage/29.shtml
這可以理解為是一種映射關系。只需要修改路由,下面是我的處理方法:
添加一條路由規則:
routes.MapRoute( name: "getnewspage", url: "newspage/{id}", defaults: new { controller = "News", action = "Details", id = UrlParameter.Optional } );
然后在News控制器中的Details正進行處理,使其訪問29.shtml這個頁面即可。
29.shtml中的內容如下:
訪問http:127.0.0.1/News/Detials/29 時即可訪問29.shtml這個文件