C#.NET動態頁面靜態化生成


一,動態頁面生成靜態也的思路是怎樣呢?

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$</title>
</head>
<body>
    <div>  <h1>標題:$sgin$</h1></div>
    <div>
        內容開始:$content$
    </div>
    <div>
        作者:$author$
    </div>
    <div>時間:$time$</div>
    <div>結束</div>
</body>
</html>

那代碼中的$content$等標識是用來替換的標識

2》我們建一個MVC項目,在HomeControllor中的代碼,如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace DynamicCreateStaticHtml.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string htmlName = WriteFile(new HtmlModel()
            {
                Title = "生成靜態頁面",
                Content = "動態自動生成靜態頁面並賦值的方法",
                Author = "admin",
                Time = DateTime.Now.ToString(),
                Sgin = "生成靜態頁面"
            });
            if (!string.IsNullOrWhiteSpace(htmlName))
            {
                return Redirect("/StaticHtml/" + htmlName + ".html");
            }
            else
            {
                return Content("生成頁面出錯");
            }
        }
        /// <summary>
        /// 動態生成靜態方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string WriteFile(HtmlModel model)
        {
            //獲取當前項目的文檔物理路徑,用於生產靜態HTML頁面存地址

            string path = Server.MapPath("./StaticHtml/");
            //gb2312簡體中文編碼
            Encoding code = Encoding.GetEncoding("gb2312");
            // 讀取模板文件,在項目下的文件
            string temp = Server.MapPath("/StaticHtml/HtmlTemp.html");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            string htmlfilename = Guid.NewGuid().ToString();
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 讀取文件
                //替換內容,模板文件已經讀入到名稱為str的變量中了
                str = str.Replace("$title$", model.Title); //模板頁中的title
                str = str.Replace("$sgin$", model.Sgin); //模板頁中的sgin
                str = str.Replace("$content$", model.Content); //模板頁中的content
                str = str.Replace("$author$", model.Author);//模板頁中的author
                str = str.Replace("$time$", model.Time); //模板頁中的time
                // 寫文件
                sw = new StreamWriter(path + htmlfilename + ".html", false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                sw.Close();
                sr.Close();  //必須關閉靜態文件鏈接,要不然會報錯
            }
            return htmlfilename;
        }
        /// <summary>
        /// 替換實體
        /// </summary>
        public class HtmlModel
        {
            public string Title { get; set; }
            public string Sgin { get; set; }
            public string Content { get; set; }
            public string Author { get; set; }
            public string Time { get; set; }
        }
    }
}

3》由以上代碼我們可以看到,我們將靜態模板的標識替換成我們要顯示的標識,然后返回這個頁面,這也是動態頁面靜態話生成的核心思路,但是我們要注意模板的文件連接需要close,要不然可能會導致文件已占用的錯誤

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM