Razor模板引擎

安稳与你 提交于 2020-03-23 07:39:55

1  Razor模板引擎的使用:

    (1)常用三种模板引擎:

      Razor 解释执行,微软内置、有提示,与JavaScript存在兼容性;

      Nvelocity / Vtemplate 运行时动态执行,(比Razor更好)。

    (2)Razor引擎的使用:

      

<!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>
    <h1>胡安定</h1>
    <div>
        <h2>@Model.Name</h2>
        <h2>@Model.Age</h2>
    </div>
    <div>
        <ul>
            @for (var i = 0; i < 10;i++ )
            {
                <li>@i</li>
            }
        </ul>
    </div>
</body>
</html>
razor1.cshtml
using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Web_Cassini.Day8
{
    /// <summary>
    /// razor1 的摘要说明
    /// </summary>
    public class razor1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string path = context.Server.MapPath("~/Day8/razor1.cshtml");
            string cshtml = File.ReadAllText(path);
            string cacheName=path + File.GetLastWriteTime(path); //文件全名+文件最后修改时间,作为缓存名,保证一旦修改缓存名改变,需要重新编译生成新的程序集
            string html = Razor.Parse(cshtml, new { Name = "yzk", Age = 33 }, cacheName); //把cshtml解析为html
            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
razor1.ashx

    (3)Razor引擎的原理:

      每次Razor引擎对cshtml页面字符串 进行解析时,都会进行编译生成新的程序集,

      设定cacheName后,只要cshtml页面不发送修改,则razor解析时不会再生成新的程序集。这样就降低内存的占用和时间的消耗。

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Console_razor
{
    class Program
    {
        static void Main(string[] args)
        {
            //把这个cshtml解析10次
            for (int i = 0; i < 10;i++ )
            {
                string path = @"F:\VisualStudio_example\ExamOneself\Console_Core\Web_Cassini\Day8\razor1.cshtml";
                string cshtml = File.ReadAllText(path);
                //string html = Razor.Parse(path, null);
                string cacheName = path + File.GetLastWriteTime(path); //只要文件未修改,则缓存不会改变
                string html = Razor.Parse(cshtml, new { Name = "yzk", Age = 33 }, cacheName);
                Console.WriteLine(html);
                Console.ReadKey();
            }
            //输出解析10次过程中产生的所有程序集
            Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();
            foreach(Assembly ass in asses )
            {
                Console.WriteLine(ass.FullName+"\r\n"); 
                //发现:如果没有缓存,则每次解析都编译生成新的程序集。占用内存并消耗大量时间。
                //所以:需要缓存,只要文件未修改,则缓存不会改变,再次解析就不用再编译生成新的程序集。
            }

            Console.ReadKey();
        }
    }
}
Console_razor.csProj

 2  Razor封装和调用外部方法

    Razor封装单独将Razor解析cshtml写为一个广泛应用的方法。

    Razor外部方法:HtmlEncodedString(htmlstring) ---> 将html字符串加密后显示;

               RawString(htmlstring) ---> 将html字符串原样输出。

public class RazorHelper
    {
        /// <summary>
        /// Razor解析cshtml页面,并输出到浏览器
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="cshtmlVirtualPath">cshtml页面的虚拟路径</param>
        /// <param name="data">传递的虚拟实例</param>
        public static void RazorParse(HttpContext context, string cshtmlVirtualPath, object data)
        {
            string fullPath = context.Server.MapPath(cshtmlVirtualPath);
            string cshtml = File.ReadAllText(fullPath);
            string cacheName = fullPath + File.GetLastWriteTime(fullPath);
            string html = Razor.Parse(cshtml, data, cacheName);
            context.Response.Write(html);
        }

        /// <summary>
        /// 对html进行加密
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>加密之后的字符串</returns>
        public static HtmlEncodedString HtmlEncodedString(string htmlStr)
        {
            return new HtmlEncodedString(htmlStr);
        }

        /// <summary>
        /// 对html原样显示
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>html原来样子</returns>
        public static RawString RawString(string htmlStr)
        {
            return new RawString(htmlStr);
        }
    }
RazorHelper.cs

 3  封装HTML标签中的<CheckBox>

 

/// <summary>
        /// 拼接生成CheckBox标签
        /// </summary>
        /// <param name="name">name属性的值</param>
        /// <param name="id">id属性的值</param>
        /// <param name="isCheck">是否选中</param>
        /// <returns>CheckBox标签</returns>
        public static RawString CheckBox(string name, string id, bool isCheck)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type='checkbox' name='").Append(name).Append("' id='").Append(id).Append("' ");
            if(isCheck)
            {
                sb.Append("checked");
            }
            sb.AppendLine(" />");
            return new RawString(sb.ToString());
        }
RazorHelper.cs

4  如果@后面的表达式是string类型,则cshtml会自动进行Encoded编码输出。

 

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            bool gender=true;
            RazorHelper.RazorParse(context, "~/Day8/razorHtml.cshtml", new { Gender = gender, Html ="List<T>就是一个集合",Text="<input type='text'/>"});
        }
<body>
    <div>
        @Console_Core.Common.RazorHelper.CheckBox("gender","gender",@Model.Gender)
    </div>
    <div>
        @Console_Core.Common.RazorHelper.RawString(Model.Html) <br />
        @Console_Core.Common.RazorHelper.RawString(Model.Text) <br />
    </div>
</body>

5  封装html标签中的<DropDownList>

     int i=10,j=10;       bool f1=i==j;  //true

    object obj1=i,obj2=j;    bool f2=obj1==obj2;  //false  ==> 值类型到引用类型,涉及到装箱,装箱之后是2个不同对象

    bool f3=object.equal(obj1,obj2);  //true 

    bool f4=obj1.equal(obj2);  //true  ==> equal就是把两个的值进行比较

    所以:引用类型的比较需要用equal函数。

/// <summary>
        /// 拼接生成DropDownList下拉列表 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValue">选中的值</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
        /// <returns>DropDownList下拉列表 标签</returns>
        public static RawString DropDownList(IEnumerable list,string valuePropName,string textPropName,object selectedValue,object extendProperties)
        { 
            //<select name='' id='' >
            //<option value=''> </option> 
            //</select>
            StringBuilder sb = new StringBuilder();
            sb.Append("<select ");
            #region 拼接扩展属性
            Type extType = extendProperties.GetType();
            PropertyInfo[] props = extType.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                string extPropName = prop.Name;
                object extPropValue = prop.GetValue(extendProperties);
                sb.Append(" ").Append(extPropName).Append("='").Append(extPropValue).Append("' ");
            } 
            #endregion
            sb.AppendLine(" >");
            #region 拼接下拉选项
            foreach (object item in list)
            {
                Type itemType = item.GetType();
                PropertyInfo itemValueProp = itemType.GetProperty(valuePropName);
                object valuePropValue = itemValueProp.GetValue(item);
                PropertyInfo itemTextProp = itemType.GetProperty(textPropName);
                object textPropValue = itemTextProp.GetValue(item);
                sb.Append("<option value='").Append(valuePropValue).Append("' ");
                if(object.Equals(valuePropValue,selectedValue)) //如果当前值与选中的值相等,则selected (引用类型用equal,如果用=则是不同的实例,因为发生过装箱)
                {
                    sb.Append(" selected ");
                }
                sb.Append(">").Append(textPropValue).AppendLine(" </option> ");
            } 
            #endregion
            sb.AppendLine("</select>");
            return new RawString(sb.ToString());
        }
RazorHelper.cs
    <div>
        @Console_Core.Common.RazorHelper.DropDownList(Model.Persons.list,Model.Persons.valuePropName,Model.Persons.textPropName,Model.Persons.PersonId,new { id = "managerId", name = "manager", style = "color:red" }) <br />
    </div>
razorHtml.cshtml
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            bool gender=true;
            List<Person> list = new List<Person>() { new Person() { Id = 1, Name = "yzk" }, new Person() { Id = 2, Name = "rupeng" } };
            RazorHelper.RazorParse(context, "~/Day8/razorHtml.cshtml", new
            {
                Gender = gender,
                Html = "List<T>就是一个集合",
                Text = "<input type='text'/>",
                Persons = new
                {
                    list = list,
                    valuePropName = "Id",
                    textPropName = "Name",
                    PersonId=2
                }
            });
        }
        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
razorHtml.ashx

 

6  总结---> 完整的 RazorHelper.cs

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using RazorEngine;
using RazorEngine.Text;

namespace Console_Core.Common
{
    public class RazorHelper
    {
        /// <summary>
        /// Razor解析cshtml页面,并输出到浏览器
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="cshtmlVirtualPath">cshtml页面的虚拟路径</param>
        /// <param name="data">传递的虚拟实例</param>
        public static void RazorParse(HttpContext context, string cshtmlVirtualPath, object data)
        {
            string fullPath = context.Server.MapPath(cshtmlVirtualPath);
            string cshtml = File.ReadAllText(fullPath);
            string cacheName = fullPath + File.GetLastWriteTime(fullPath);
            string html = Razor.Parse(cshtml, data, cacheName);
            context.Response.Write(html);
        }

        /// <summary>
        /// 对html进行加密
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>加密之后的字符串</returns>
        public static HtmlEncodedString HtmlEncodedString(string htmlStr)
        {
            return new HtmlEncodedString(htmlStr);
        }

        /// <summary>
        /// 对html原样显示
        /// </summary>
        /// <param name="htmlStr">html标签</param>
        /// <returns>html原来样子</returns>
        public static RawString RawString(string htmlStr)
        {
            return new RawString(htmlStr);
        }

        /// <summary>
        /// 拼接生成CheckBox 标签
        /// </summary>
        /// <param name="isCheck">是否选中</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
        /// <returns>CheckBox标签</returns>
        public static RawString CheckBox(bool isCheck, object extendProperties)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type='checkbox' ");
            sb.Append(RenderExtProperties(extendProperties));
            if(isCheck)
            {
                sb.Append(" checked ");
            }
            sb.AppendLine(" />");
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接扩展属性 及对应的值
        /// </summary>
        /// <param name="extendProperties">扩展属性 所在的匿名实例</param>
        /// <returns>拼接生成的 包含属性名和值 的字符串: 比如,“ name='manager' id='managerId' ” </returns>
        private static string RenderExtProperties(object extendProperties)
        {
            StringBuilder sb = new StringBuilder();
            #region 拼接扩展属性
            Type extType = extendProperties.GetType();
            PropertyInfo[] props = extType.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                string extPropName = prop.Name;
                object extPropValue = prop.GetValue(extendProperties);
                sb.Append(" ").Append(extPropName).Append("='").Append(extPropValue).Append("' ");
            }
            #endregion
            return sb.ToString();
        }

        /// <summary>
        /// 拼接生成DropDownList下拉列表 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValue">选中的值</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
        /// <returns>DropDownList下拉列表 标签</returns>
        public static RawString DropDownList(IEnumerable list,string valuePropName,string textPropName,object selectedValue,object extendProperties)
        { 
            //<select name='' id='' >
            //<option value=''> </option> 
            //</select>
            StringBuilder sb = new StringBuilder();
            sb.Append("<select ");
            #region 拼接扩展属性
            sb.Append(RenderExtProperties(extendProperties));
            #endregion
            sb.AppendLine(" >");
            #region 拼接下拉选项
            foreach (object item in list)
            {
                object valuePropValue, textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<option value='").Append(valuePropValue).Append("' ");
                if(object.Equals(valuePropValue,selectedValue)) //如果当前值与选中的值相等,则selected (引用类型用equal,如果用=则是不同的实例,因为发生过装箱)
                {
                    sb.Append(" selected ");
                }
                sb.Append(">").Append(textPropValue).AppendLine(" </option> ");
            } 
            #endregion
            sb.AppendLine("</select>");
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接生成RadioButtonList 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValue">选中的值</param>
        // <param name="extendProperties">扩展属性的对象:比如,new {name='gender',style='color:red' }</param>
        /// <returns>RadioButtonList 标签</returns>
        public static RawString RadioButtonList(IEnumerable list, string valuePropName, string textPropName, object selectedValue, object extendProperties)
        {
            //<input type="radio" name="gender" value="1" checked /><label>男</label><br />  //只能单选
            StringBuilder sb = new StringBuilder();
            foreach(object item in list)
            {
                object valuePropValue, textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<input type=\"radio\" ");
                sb.Append(RenderExtProperties(extendProperties));
                sb.Append(" value=\"").Append(valuePropValue).Append("\"");
                if(object.Equals(valuePropValue,selectedValue))
                {
                    sb.Append(" checked ");
                }
                sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
            }
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 拼接生成CheckBoxList 标签
        /// </summary>
        /// <param name="list">实例的集合</param>
        /// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
        /// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
        /// <param name="selectedValues">选中的值的数组</param>
        /// <param name="extendProperties">扩展属性的对象:比如,new {name='hobby',style='color:red' }</param>
        /// <returns>CheckBoxList 标签</returns>
        public static RawString CheckBoxList(IEnumerable list, string valuePropName, string textPropName, object[] selectedValues, object extendProperties)
        {
            //<input type="checkbox" name="hobby" value="1" checked /><label>足球</label><br />   //可多选
            StringBuilder sb = new StringBuilder();
            foreach(object item in list)
            {
                object valuePropValue,textPropValue;
                GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
                sb.Append("<input type=\"checkbox\" ");
                sb.Append(RenderExtProperties(extendProperties));
                sb.Append (" value=\"").Append(valuePropValue).Append("\" ");
                if(selectedValues.Contains(valuePropValue))
                {
                    sb.Append(" checked ");
                }
                sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
            }
            return new RawString(sb.ToString());
        }

        /// <summary>
        /// 根据指定实例的 值属性名和文本属性名 获得 值属性值和文本属性值
        /// </summary>
        /// <param name="item">指定实例</param>
        /// <param name="valuePropName">值属性名</param>
        /// <param name="textPropName">文本属性名</param>
        /// <param name="valuePropValue">out 值属性值</param>
        /// <param name="textPropValue">out 文本属性值</param>
        private static void GetvalueAndTextPropValue(object item, string valuePropName, string textPropName, out object valuePropValue, out object textPropValue)
        {
            Type type = item.GetType();
            PropertyInfo valueProp = type.GetProperty(valuePropName);
            valuePropValue = valueProp.GetValue(item);
            PropertyInfo textProp = type.GetProperty(textPropName);
            textPropValue = textProp.GetValue(item);
        }
    }
}
RazorHelper.cs
<!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>
    <div>
        @Console_Core.Common.RazorHelper.CheckBox(@Model.Gender, new { name="gender",id="genderId"})
    </div>
    <div>
        @Console_Core.Common.RazorHelper.RawString(Model.Html) <br />
        @Console_Core.Common.RazorHelper.RawString(Model.Text) <br />
    </div>
    <div>
        @Console_Core.Common.RazorHelper.DropDownList(Model.Persons.list,Model.Persons.valuePropName,Model.Persons.textPropName,Model.Persons.PersonId,new { id = "managerId", name = "manager", style = "color:red" }) <br />
    </div>
    <div>
        @*<input type="radio" name="gender" value="1" / checked><label>男</label><br />*@
        @Console_Core.Common.RazorHelper.RadioButtonList(Model.Genders.list, Model.Genders.valuePropName, Model.Genders.textPropName, Model.Genders.selectedValue, new { name="gender",style = "color:blue" }) <br />
    </div>
    <div>
        @*<input type="checkbox" name="hobby" value="1" checked /><label>足球</label><br />*@
        @Console_Core.Common.RazorHelper.CheckBoxList(Model.Hobbys.list, Model.Hobbys.valuePropName, Model.Hobbys.textPropName, Model.Hobbys.selectedValues, new { name="hobby",style = "color:yellow" }) <br />
    </div>
</body>
</html>
razorHtml.cshtml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Console_Core.Common;

namespace Web_Cassini.Day8
{
    /// <summary>
    /// razorHtml 的摘要说明
    /// </summary>
    public class razorHtml : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            bool gender=true;
            List<Person> list = new List<Person>() { new Person() { Id = 1, Name = "yzk" }, new Person() { Id = 2, Name = "rupeng" } };
            List<Gender> listGender = new List<Gender>() { new Gender() { Id = 1, Name = "男" }, new Gender() { Id = 2, Name = "女"},new Gender() { Id = 3, Name = "太监" }};
            List<Hobby> listHobby = new List<Hobby>() { new Hobby() { Id = 1, Name = "足球" }, new Hobby() { Id = 2, Name = "篮球" }, new Hobby() { Id = 3, Name = "排球" } };
            RazorHelper.RazorParse(context, "~/Day8/razorHtml.cshtml", new
            {
                Gender = gender,
                Html = "List<T>就是一个集合",
                Text = "<input type='text'/>",
                Persons = new
                {
                    list = list,
                    valuePropName = "Id",
                    textPropName = "Name",
                    PersonId=2
                },
                Genders = new 
                {
                    list = listGender,
                    valuePropName = "Id",
                    textPropName = "Name",
                    selectedValue=2,
                },
                Hobbys = new 
                {
                    list = listHobby,
                    valuePropName = "Id",
                    textPropName = "Name",
                    selectedValues=new object[]{1,3}
                }
            });
        }
        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Gender
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Hobby
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
razorHtml.ashx      
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!