模版引擎RazorEngine簡介


ASP.NET MVC的Razor想必大家都比較熟悉,這里介紹一個獨立於ASP.NET的RazorEngine。

RazorEngine是一個開源的項目,它的基礎就是ASP.NET MVC的Razor。GitHub項目地址

您可以在Windows Console或者Windows Forms使用它。

下面簡單介紹如何使用。

1.創建一個Windows Console

2.通過NuGet安裝RazorEngine

3.下面先介紹使用字符串替代cshtml文件模板的代碼。

using RazorEngine;
using RazorEngine.Templating;
namespace RazorDemo.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var template = "Hello @Model.Name, welcome to use RazorEngine!";
            var result = Engine.Razor.RunCompile(template, "templateKey1", null, new { Name = "World" });
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

運行程序,查看結果。

 

4.下面是使用cshtml文件作為模板的代碼。

4.1添加一個Model文件夾,然后添加一個類UserInfo作為Model。

namespace RazorDemo.ConsoleDemo.Model
{
    public class UserInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

  

4.2添加一個文件夾Templates,然后添加一個html文件,將擴展名改為cshtml。這個文件屬性改為內容Content,總是拷貝Copy always。

@model RazorDemo.ConsoleDemo.Model.UserInfo
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Welcome</title>
</head>
<body>
    <p>Hi @Model.FirstName @Model.LastName,</p>
    <p>Welcome to use RazorEngine!</p>
</body>
</html>

 

4.3最后修改Main方法的代碼。

using RazorEngine;
using RazorEngine.Templating;
using RazorDemo.ConsoleDemo.Model;
namespace RazorDemo.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + "templates\\hello.cshtml";
            var template = File.ReadAllText(path);
            var model = new UserInfo { FirstName = "Bill", LastName = "Gates" };
            var result = Engine.Razor.RunCompile(template, "templateKey2", null, model);
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

運行程序,查看結果。

運行程序的時候Console可能會輸出一大串有關臨時文件的信息,可以忽略。如果想去掉,請參考這里

其實在后台RazorEngine將模板和模型結合后生成了一個對應的臨時類文件,然后調用Execute方法生成最終結果。

namespace CompiledRazorTemplates.Dynamic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    [RazorEngine.Compilation.HasDynamicModelAttribute()]
    public class RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a : RazorEngine.Templating.TemplateBase<RazorDemo.ConsoleDemo.Model.UserInfo>
    {
        public RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a()
        {
        }
        public override void Execute()
        {
            WriteLiteral("<!DOCTYPE html>\r\n\r\n<html");
            WriteLiteral(" lang=\"en\"");
            WriteLiteral(" xmlns=\"http://www.w3.org/1999/xhtml\"");
            WriteLiteral(">\r\n<head>\r\n    <meta");
            WriteLiteral(" charset=\"utf-8\"");
            WriteLiteral(" />\r\n    <title>Welcome</title>\r\n</head>\r\n<body>\r\n    <p>Hi ");
            Write(Model.FirstName);
            WriteLiteral(" ");
            Write(Model.LastName);
            WriteLiteral(",</p>\r\n    <p>Welcome to use RazorEngine!</p>\r\n</body>\r\n</html>\r\n");
        }
    }
}

  

RazorEngine的用途很多,只要有模板(cshtml)和模型(Model)就可以使用。比如

1)制作一個代碼生成器

2)生成郵件正文

 

如果不妥之處,請見諒。

 


免責聲明!

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



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