項目練習:自己寫一個讀取指定html文件的Razor

項目要求

練習2@RPHelper.Include("~/1.html")
把~/1.html內容原樣輸出到這個位置

是項目    ProjectLX001

第一步:假定讀取的html文件是這個

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <input type="text" id="article" name="article" />
    <br /><br />
    <input type="checkbox" id="hobby" name="hobby" />
    <br /><br />
    <select id="xingqu" name="xingqu">
        <option value="1">C語言</option>
        <option value="2">Java語言</option>
        <option value="3">C#語言</option>
        <option value="4">php語言</option>

    </select>

    <input type="button" value="提交" />
</body>
</html>

第二步:寫類HtmlHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RazorEngine;
using System.IO;
using RazorEngine.Text;

namespace ProjectLX001
{
    public class HtmlHelper
    {
        //1.封裝一個ParseRazor方法,避免每次都爲動態程序集生成別名
        public static string ParseRazor(HttpContext context, string csHtmlVirtualPath, object model)
        {
            //1.1 拿到模板文件的 的虛擬路徑
            string fullPath = context.Server.MapPath(csHtmlVirtualPath);
            //1.2 讀取模板文件csHtml
            string cshtml = File.ReadAllText(fullPath);
            //1.3 給模板文件生成的動態程序集起一個別名字,防止,重複編譯生成動態程序集,影響網站的速度性能
            //這裏用時間
            string cacheName = fullPath + File.GetLastWriteTime(fullPath);
            //1.4 用model替換模板中變量值的內容
            string html = Razor.Parse(cshtml, model, cacheName);
            return html;
        }


        //2.封裝一個OutHtml方法,將1.html文件讀取
        public static RawString Include(HttpContext context, string vitualHtmlPath)
        {
            //2.1 取得html的虛擬路徑
            string fullHtmlPath = context.Server.MapPath(vitualHtmlPath);
            //2.2 IO讀取html文件中的內容
            string html = File.ReadAllText(fullHtmlPath);
            //2.3 返回
            return new RawString(html);

        }
    }
}

第三步:寫模板文件1.cshtml


<!--添加HtmlHelper的命名空間-->
@using ProjectLX001

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    @HtmlHelper.Include();
</body>
</html>

第四步:讀取模板文件一般處理程序

using RazorEngine.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectLX001
{
    /// <summary>
    /// _1 的摘要說明
    /// </summary>
    public class _1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";//1.修改爲html

            //2.讀取cshtml模板
            string html = HtmlHelper.ParseRazor(context,"~/1.html",null);

            //3.返回瀏覽器端
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

運行結果

這裏寫圖片描述
這裏寫圖片描述

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