逃脱Asp.Net MVC框架的枷锁,使用Razor视图引擎

为什么要这么做?
1.  Asp.Net MVC 其实也不是太好
2. 我有自己的敏捷Web框架, 仍然想用Razor引擎
3. 动态编译很有意思,这也是将来的一个趋势,如果有人有兴趣,我倒是很想写写这方面的内容.
可是也有这些想法的人并不多,找来找去,这方面的资料和论述极其之少。与其临渊羡鱼,不如退而结网。自己动手,丰衣足食。

如Razor引擎浅析所述,Razor的两大主要功能,模板文件和动态编译器。我们能不能单纯就要而且只要这俩主要特性? 其它那些智能查找视图文件等,虽不能说是垃圾,也是束缚手脚的条条框框,我完全可以自己做,失去的是一条绳索,获得是整个世界。
保持模板功能,特别是设计时智能语法支持,很简单,创建项目时选择MVC项目,然后把其它MVC相关的引用删掉只剩下Razor就好了。
编译? 你根本就找不到这一段代码,被淹没在大量的辅助代码中。幸好,我这里已经找到了,简化修改之后,就剩下了了几行,你就可以直接使用在甚至不必是Web项目中。
 
首先, 用到的Razor域名空间。

1 using System.Web.Razor;

 

2 using System.Web.Razor.Generator;

 

3 using System.Web.Razor.Parser;

 

第一步,动态编译:解析视图文件,生成代码,是的,生成代码,先。Razor的 语法可以说是私有语法,需要先生成标准代码,然后才编译,生成我们熟悉的C#类Type。需要注意的是,我下面代码用的模板基类是我自己的 TeamplateBase,后面会给出简单实现,当然,好处就是灵活性。你也可以直接用Asp.Net MVC的System.Web.Mvc.WebViewPage, 不过我没有试过,也许会有其他问题,不能保证。

01 public static Type Compile<T>(string template_path)

 

02         {

 

03             //准备临时类名,读取模板文件和Razor代码生成器

 

04             var class_name = "c" + Guid.NewGuid().ToString("N");

 

05             var base_type = typeof(TemplateBase<>).MakeGenericType(typeof(T));

 

06             var template = File.ReadAllText(template_path);

 

07  

 

08             var host = new RazorEngineHost(new CSharpRazorCodeLanguage(), () => new HtmlMarkupParser())

 

09                            {

 

10  

 

11                                DefaultBaseClass = base_type.FullName,

 

12                                DefaultClassName = class_name,

 

13                                DefaultNamespace = "YourNameSpace.dynamic",

 

14                                GeneratedClassContext =

 

15                                    new GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo",

 

16                                                              "WriteLiteralTo",

 

17                                                              "YourNameSpace.TemplateBase")

 

18  

 

19                            };

 

20             host.NamespaceImports.Add("System");

 

21             host.NamespaceImports.Add("YourNameSpaces");

 

22              

 

23             //生成代码

 

24             CodeCompileUnit code;

 

25             using (var reader = new StringReader(template)) {

 

26                 var generatedCode = new RazorTemplateEngine(host).GenerateCode(reader);

 

27                 code = generatedCode.GeneratedCode;

 

28             }

 

29             //准备编译参数

 

30             var @params = new CompilerParameters

 

31             {

 

32                 IncludeDebugInformation = false,

 

33                 TempFiles = new TempFileCollection(AppDomain.CurrentDomain.DynamicDirectory),

 

34                 CompilerOptions = "/target:library /optimize",

 

35                 GenerateInMemory = false

 

36             };

 

37  

 

38             var assemblies = AppDomain.CurrentDomain

 

39                .GetAssemblies()

 

40                .Where(a => !a.IsDynamic)

 

41                .Select(a => a.Location)

 

42                .ToArray();

 

43             @params.ReferencedAssemblies.AddRange(assemblies);

 

44  

 

45             //编译

 

46             var provider = new CSharpCodeProvider();

 

47             var compiled = provider.CompileAssemblyFromDom(@params, code);

 

48  

 

49             if (compiled.Errors.Count > 0) {

 

50                 var compileErrors = string.Join("\r\n", compiled.Errors.Cast<object>().Select(o => o.ToString()));

 

51                 throw new ApplicationException("Failed to compile Razor:" + compileErrors);

 

52             }

 

53  

 

54             //编译成功后, 返回编译后的动态Type

 

55             return compiled.CompiledAssembly.GetType("Skight.Arch.Presentation.Web.Core.ViewEngins.Razor.dynamic." + class_name);

 

56  

 

57  

 

58         }

 

第二步就简单多了,就和任何静态类一样,用反射创建实例,然后复制Model对象执行模板,最后输出结果是,就自动吧Model类的数据嵌入了。
 

01 public static string Render<T>(T model,string template_path)

 

02         {

 

03             var type = Compile<T>(template_path);

 

04             //创建视图实例

 

05             var instance = (TemplateBase<T>)Activator.CreateInstance(type);

 

06  

 

07             //执行模板(把数据嵌入文件)

 

08             instance.Model = model;

 

09             instance.Execute();

 

10             //输出最终结果

 

11             var result = instance.Result;

 

12  

 

13             return result;

 

14         }

 

最后,看看视图模板类,一个基类和一个泛型基类,后者用于前类型Model

view sourceprint?

01 public abstract class TemplateBase

 

02     {

 

03         public string Layout { get; set; }

 

04         public UrlHelper Url { get; set; }

 

05         public Func<string> RenderBody { get; set; }

 

06         public string Path { get; internal set; }

 

07         public string Result { get { return Writer.ToString(); } }

 

08  

 

09         protected TemplateBase()

 

10         {

 

11         }

 

12  

 

13         public TextWriter Writer

 

14         {

 

15             get

 

16             {

 

17                 if(writer==null)

 

18                 {writer = new StringWriter();

 

19                 }

 

20                 return writer;

 

21             }

 

22             set {

 

23                 writer = value;

 

24             }

 

25         }

 

26  

 

27         private TextWriter writer;

 

28  

 

29         public void Clear() {

 

30            Writer.Flush();

 

31         }

 

32  

 

33         public virtual void Execute() { }

 

34  

 

35         public void Write(object @object) {

 

36             if (@object == null) {

 

37                 return;

 

38             }

 

39             Writer.Write(@object);

 

40         }

 

41  

 

42         public void WriteLiteral(string @string) {

 

43             if (@string == null) {

 

44                 return;

 

45             }

 

46             Writer.Write(@string);

 

47         }

 

48  

 

49         public static void WriteLiteralTo(TextWriter writer, string literal) {

 

50             if (literal == null) {

 

51                 return;

 

52             }

 

53             writer.Write(literal);

 

54         }

 

55  

 

56         public static void WriteTo(TextWriter writer, object obj) {

 

57             if (obj == null) {

 

58                 return;

 

59             }

 

60             writer.Write(obj);

 

61         }

 

62     }

 

63     public abstract class TemplateBase<T> :TemplateBase

 

64     {

 

65         public T Model { get; set; }            

 

66     }

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku