是其产生使用ASP.NET MVC查看电子邮件模板,而不必赴汤蹈火的解决方案。
Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.
让我解释铁圈跳。
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse); var oldContext = HttpContext.Current; HttpContext.Current = fakeContext; var html = new HtmlHelper(new ViewContext(fakeControllerContext, new FakeView(), viewDataDictionary, new TempDataDictionary()), new ViewPage()); html.RenderPartial(viewName, viewData, viewDataDictionary); HttpContext.Current = oldContext;的上面code是使用当前的HttpContext伪造一个新的语境和渲染的RenderPartial页,我们不应该这样做。的
使用ControllerContext和.Render另一个非常详细的解决方案:( IEmailTemplateService,页眉/回发解决方法),但pretty很多做同样的事情有很多更code。
Another very detailed solution using ControllerContext and .Render: (IEmailTemplateService, Headers/Postback WorkAround) but pretty much doing the same thing with a lot more code.
我,另一方面,我寻找的东西只会渲染视图,而无需POST / GET和我产生一个简单的字符串,我可以通过我的电子邮件code送行。一些不遇到错误,如发布标题两次或伪造某些数据。
I on the other hand, am looking for something that would just render a View without having to POST/GET and generates me a simple string that I can send off through my Email code. Something that doesn't run into errors such as posting headers twice or faking some piece of data.
EX:
//code which does not fire Render, RenderPartial... etc var email = emailFramework.Create(viewData, view);请参阅我的解决方案波纹管或请点击此链接:
See my solution bellow or follow this link:
我的解决方案使用火花:(12/30/2009) ASP.NET MVC的电子邮件模板解决方案
My Solution using spark: (12/30/2009) ASP.NET MVC Email Template Solution
推荐答案这就是我想要的ASP.NET MVC的ViewEngine的事,但它是在星火,只需按照最新的链接权波纹管,
This is what I wanted the ASP.NET MVC ViewEngine to do, but it's in Spark, just follow the latest link right bellow,
更新(2009/12/30)清洁版本:的 ASP.NET MVC的电子邮件模板解决方案
(11/16/2009)或路易斯DeJardin控制台应用程序版本:
(11/16/2009) Or, Louis DeJardin Console Application Version:
using System; using Spark; using Spark.FileSystem; public class User { public int Id { get; set; } public string Name { get; set; } } public abstract class EmailView : AbstractSparkView { public User user { get; set; } } class Program { static void Main(string[] args) { // following are one-time steps // create engine var settings = new SparkSettings() .SetPageBaseType(typeof(EmailView)); var templates = new InMemoryViewFolder(); var engine = new SparkViewEngine(settings) { ViewFolder = templates }; // add templates templates.Add("sample.spark", @"Dear ${user.Name}, This is an email.Sincerely, Spark View Engine constanto/unsubscribe/${user.Id}"); // following are per-render steps // render template var descriptor = new SparkViewDescriptor() .AddTemplate("sample.spark"); var view = (EmailView)engine.CreateInstance(descriptor); view.user = new User { Id = 655321, Name = "Alex" }; view.RenderView(Console.Out); Console.ReadLine(); } }我决定,因为它似乎是一个没有一切权利使用这种方法,它:
I decided to use this method because it seems to be the one that does everything right, it:
- 不使用任何的HttpContext / ControllerContext或路由数据一团糟!
- 它可以实现页眉/页脚,让模板!
- 您可以使用循环,条件等...
- 它的清洁,重量轻,特别是如果你打算完全转移到火花视图引擎!
请,请务必阅读这些职位。所有信用卡路易DeJardin看到他的教程:):使用星火作为一种通用的模板引擎! ,电子邮件模板再探
Please, make sure to read these posts. All credit to Louis DeJardin see his tutorials :): Using Spark as a general purpose template engine!, Email Templates Revisited
更多推荐
ASP.NET MVC电子邮件
发布评论