使用shtml實現.netmvc頁面靜態化

引用自:http://www.cnblogs.com/catcher1994/p/5115237.html 轉發防丟失

ASP.NET MVC使用SSI來實現頁面靜態化

頁面靜態化分爲兩種:僞靜態和真靜態,這裏主要介紹的是真靜態。

進入正題之前先簡單介紹一下SSI和shtml:

1)、SSI是Server Side Include的簡稱(服務器端嵌入)

2)、shtml是包含有嵌入式服務器方包含命令的HTML文本。在被傳送給瀏覽器之前,服務器會對shtml文檔進行

完全的讀取、分析以及修改。

想要讓IIS支持服務器包含,還需要簡單配製一下,打開服務器端包含即可。啓用或關閉Windows功能->
Internet Information Services->萬維網服務->應用程序開發功能->服務器端包含。
![在這裏插入圖片描述](https://images2015.cnblogs.com/blog/558945/201601/55
然後打開IIS,在主頁上找到處理程序映射
在這裏插入圖片描述

啓用*.shtm和*.shtml

實現這個主要就是主要就是生成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這個文件
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章