问题描述
我试图使用作为电子邮件模板引擎。我想借此与数据模型,组装一套相应的部分景色,并返回HTML,我可以通过电子邮件发送出去。结果
修改除了这个工作流程,任何解决方案将需要可编辑的.cshtml文件的能够被编译成一个dll 的。由于各种原因,这是不实际的部署CSHTML文件本身 - 如果我们不能嵌入我们所有的剃须刀意见纳入一个单一的文件,那么我们就不能使用这一建议。因此RazorGenerator。
I'm trying to use RazorGenerator as an email template engine. I want to take a model with the data, assemble the correct set of partial views, and return HTML that I can email out.
In addition to this workflow, any solution would need to be editable as a .cshtml file and be able to be compiled into a dll. For various reasons, it is not practical to deploy the cshtml files themselves - if we can't embed all our razor views into a single file, then we can't use that suggestion. Hence RazorGenerator.
到目前为止,我已经计算出它的每一个部分,除了谐音。当我尝试使用@ Html.Partial()在模板文件,我得到:HTML这个名字并不在目前的情况下
存在。
So far, I've worked out every part of it, except for the partials. When I try to use @Html.Partial() in a template file, I get: The name 'Html' does not exist in the current context
.
,我知道 @Html
默认情况下不剃刀的一部分,而且有许多答案在那里就如何在控制器中创建一个的HtmlHelper
。不过,我需要创建一个在模板中,它不具备 ControllerContext
,我就需要跟着这些例子。
Based on this answer, I know that @Html
isn't part of Razor by default, and there's many answers out there as to how to create a HtmlHelper
in a controller. However, I need to create one in a template, which doesn't have the ControllerContext
that I'd need to follow those examples.
我也使用@include试过,但RazorGenerator模板似乎并不支持这一点。 修改我也尝试建立从继承一个新的类 TemplateBase<>
和复制的所有功能 RazorTemplateBase
,但我得到的 @include
行的NullReferenceException
秒。
I've also tried using @Include, but the RazorGenerator template doesn't seem to support that. I also tried creating a new class which inherited from TemplateBase<>
and copied all the functionality of RazorTemplateBase
, but I get NullReferenceException
s on the @Include
line.
所以,我的主要问题是:有没有更好的办法,包括另一个剃刀文件(模型)到我的文件
So, my primary question is: Is there a better way to include another Razor file (with model) into my file?
其次,如果没有更好的办法,我怎么能得到的HtmlHelper创建?
Secondarily, if there isn't a better way, how can I get the HtmlHelper created?
修改为赏金:只是为了重申一下,四件事情,我需要在可以接受的答案是:
Edit for bounty: Just to reiterate, the four things I need in an acceptable answer are:
- 要编辑与标准编辑器(没有将其存储为一个字符串或此类)
.cshtml
文件的能力 - 要编译一切都变成可使用我们当前构建系统部署的单个DLL的能力(我们不能部署大量个人.cshtml年代)
- 来从另一个引用一个.cshtml文件,并通过模型的能力 - 这相当于
@Includes
或@ Html.Partial
(无论其是如果他们的工作完全可以接受的) - 要通过电子邮件发送出来的结果,带有附件的能力。 (我已经有code代表此,如果结果是字符串或可转换为一个。)
- The ability to edit
.cshtml
files with the standard editor (no "store it as a string" or such) - The ability to compile everything into a single dll which can be deployed using our current build system (we can't deploy lots of individual .cshtml's)
- The ability to reference one .cshtml file from another, and pass a model - equivalent to
@Includes
or@Html.Partial
(Either of which are perfectly acceptable if they work) - The ability to email out the result, with attachments. (I already have code for this, if the result is a string or convertible to one.)
我目前能得到这三个工作的大多数组合,但我不能让所有四个一次。我打开新的图书馆,更换RazorGenerator,或扔出去的东西我已经有任何部分,只要结果可以作为必要的。
I currently can get most combinations of three of these working, but I can't get all four at once. I'm open to new libraries, replacing RazorGenerator, or throwing out any part of what I already have, so long as the result works as needed.
推荐答案
只是一个想法,但为什么不能在设置其他页面,并在控制器code打开一个HttpWebRequest的/ Web客户端发送所需要的数据在那里,让所有的HTML /文指出的看法,几个电话合并在一起,然后通过电子邮件发送出来的所有字符串。
Just a thought, but why couldn't you set up other pages and in your Controller Code open up an HTTPWebRequest / WebClient send the data you need there, get all the html/text out of that view, merge several calls together and then email out all that string.
public ActionResult SomeAction() {
// call other section logic using HttpWebRequest or WebClient
// /controller/emailsection/{vars}/......
// Get the string out of the request add it to ViewData["xxx"]
// rinse and repeat for other sections
}
public ActionResult EmailSection() {
//put section logic here
Response.ContentType = "text/html"; // "text/plain"
Response.Write("Some HttpWebResponse");
return null;
}
这篇关于RazorGenerator,模板和@Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!