使用视图模型托管的Razor视图引擎 [英] Hosting the Razor View Engine using a view model

查看:86
本文介绍了使用视图模型托管的Razor视图引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的Razor视图引擎ASP.NET MVC之外生成HTML的邮件,我喜欢的语法和似乎没有必要使用其他模板引擎的时候我已经在我的项目剃刀。

I'd like to use the Razor View Engine outside of ASP.NET MVC to generate HTML for emails, I like the syntax and it seems unnecessary to use another templating engine when I already have Razor in my project.

所以,我环顾四周,发现如何做到这本指南..
http://blog.andrewnurse.net/2010/11/16/HostingRazorOutsideOfASPNetRevisedForMVC3RC.aspx

So I looked around and found this guide on how to do it.. http://blog.andrewnurse.net/2010/11/16/HostingRazorOutsideOfASPNetRevisedForMVC3RC.aspx

不幸的是我无法找到指定一个视图模型,这是可悲的任何方式,因为我真的,真的喜欢有强类型的意见,甚至对我的电子邮件。

Unfortunately I can't find any way of specifying a view model, which is sad because I would really, really like to have strongly typed views even for my emails.

那么,有分析剃刀模板ASP.NET MVC以外使用强类型视图模型的任何方式还是那么麻烦它不值得冒这个险?

So is there any way of parsing Razor templates outside of ASP.NET MVC with strongly typed view models or is it so much trouble it's not worth the hassle?

推荐答案

使用@model标签实际上是为@inherits标签的快捷方式。

Using the @model tag is actually a shortcut for the @inherits tag.

您指定类,您生成的类会从与@inherits指定的类继承。

You specify the class, your generated class will inherit from from the class specified with @inherits.

因此​​,如果您指定 @inherits MyTemplate的<&为MyModel GT;

So if you specify @inherits MyTemplate<MyModel>

MyTemplate的应该是这样的:

MyTemplate should look like:

class MyTemplate<T> {
    public T Model { get; set; }

    public abstract void Execute();
    public virtual void Write(object value) {
        WriteLiteral(value);
    }

    public virtual void WriteLiteral(object value) {
        // Actual writing goes here
    }

}

从剃刀解析结果,你需要编译,并创建一个实例。

The result from the razor parsing, you need to compile, and create an instance from.

您创造了你可以设置模型属性,调用execute生成结果的实例后,怎么和你产生什么给你。

After you created the instance you can set the Model property, and call Execute to generate the result, how and what you generate is up to you.

这篇关于使用视图模型托管的Razor视图引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆