ASP.NET MVC电子邮件 [英] ASP.NET MVC Email

查看:201
本文介绍了ASP.NET MVC电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是使用ASP.NET MVC视图生成电子邮件模板的解决方案,而不必跳过环。



让我详细说一下跳过环。 p>

  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;

上面的代码是使用当前的HttpContext伪造一个新的上下文,并渲染页面RenderPartial,我们不应该这样做。



另一个使用ControllerContext和.Render的非常详细的解决方案:
IEmailTemplateService,标题/ Postback WorkAround ),但几乎做同样的事情与更多的代码。



另一方面,我正在寻找一些可以渲染视图的东西,而无需POST / GET,并生成一个简单的字符串,可以通过我的电子邮件代码发送。
不会发生错误的事情,例如发布标题两次或伪造一些数据。



EX:

  //不启动的代码Render,RenderPartial ... etc 
var email = emailFramework.Create(viewData,view );






请查看我的解决方案,或按照以下链接: / p>

我的解决方案使用spark:(12/30/2009)

这就是我想要的ASP.NET MVC ViewEngine做,但它是在Spark,只是按照最新的链接右下角,



更新(12/30/2009)Cleaner版本: ASP.NET MVC电子邮件模板解决方案






(11/16/2009)或者,Louis DeJardin控制台应用程序版本:

  using System; 
使用Spark;
使用Spark.FileSystem;

public class User
{
public int Id {get;组; }
public string Name {get;组;
}

public abstract class EmailView:AbstractSparkView
{
public User user {get;组;
$

class程序
{
static void Main(string [] args)
{
//以下是一次性步骤

//创建引擎
var settings = new SparkSettings()
.SetPageBaseType(typeof(EmailView));

var templates = new InMemoryViewFolder();
var engine = new SparkViewEngine(settings)
{
ViewFolder = templates
};

//添加模板
templates.Add(sample.spark,@Dear $ {user.Name},这是一封电子邮件。诚然,Spark View Engine http:// constanto.org/unsubscribe/${user.Id});

//以下是每个渲染步骤

//渲染模板
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();
}
}

我决定使用这种方法,因为它似乎是没有使用任何HttpContext / ControllerContext或乱码路由数据!

$ b


  • $ b
  • 它可以实现标题/页脚以允许模板!

  • 您可以使用循环,条件等...

  • 它是干净的,轻便,特别是如果你打算完全移动到火花浏览引擎!



请确保阅读这些帖子。所有信用路易斯·德雅丁看到他的教程:):
使用Spark作为通用模板引擎!电子邮件模板重访


Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.

Let me elaborate jumping 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;

The above code is using the current HttpContext to fake a new Context and render the page with RenderPartial, we shouldn't have to do this.

Another very detailed solution using ControllerContext and .Render: (IEmailTemplateService, Headers/Postback WorkAround) but pretty much doing the same thing with a lot more 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:

My Solution using spark: (12/30/2009) ASP.NET MVC Email Template Solution

解决方案

This is what I wanted the ASP.NET MVC ViewEngine to do, but it's in Spark, just follow the latest link right bellow,

Update (12/30/2009) Cleaner Version: ASP.NET MVC Email Template Solution


(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 http://constanto.org/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:

  • Does not use any HttpContext/ControllerContext or mess with routing data!
  • It can implement Header/Footer to allow templates!
  • You can use loops, conditionals, etc...
  • It's clean, light weight especially if you plan to move entirely to spark view engine!

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电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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