发送电​​子邮件里面asp.net的MVC作用的结果 [英] Send asp.net mvc action result inside email

查看:249
本文介绍了发送电​​子邮件里面asp.net的MVC作用的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我在asp.net的MVC行动,作为模板引擎,结果字符串的形式,我可以在邮件发送。

伪code:

 公众的ActionResult寄存器()
{
    SendEmail(查看()的ToString());    返回新EmptyResult();
}


解决方案

首先,你可能仍然想从你的行动返回一个视图,因此返回一个EmptyResult是不是最好的;但你会明白这一点,当你处理您的注册过程中的页面流。

我假设你想使用你已经创建了一个视图中创建的HTML电子邮件。这意味着你要承担的东西,看起来像以下结果:

 公众的ActionResult CreateEmailView(RegistrationInformation信息)
{
  VAR userInformation = Membership.CreateNewUserLol(信息);
  返回查看(userInformation)
}

和发送的电子邮件的正文。你得到重用你的意见和所有有趣的东西。

您可以通过创建自定义的ActionResult并使用此生成文本趁框架。

下面是一些C#般的伪code,可能实际编译和工作。首先,自定义的ActionResult:

 公共类StringResult:的ViewResult
{
    公共字符串的Html {搞定;组; }
    公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
    {
        如果(上下文== NULL)
        {
            抛出新的ArgumentNullException(上下文);
        }
        如果(string.IsNullOrEmpty(this.ViewName))
        {
            this.ViewName =
                 context.RouteData.GetRequiredString(行动);
        }
        ViewEngineResult结果= NULL;
        如果(this.View == NULL)
        {
            结果= this.FindView(上下文);
            this.View = result.View;
        }
        ViewContext viewContext =新ViewContext(
                背景下,this.View,this.ViewData,this.TempData);
        使用(VAR流=新的MemoryStream())
        使用(VAR作家=新的StreamWriter(流))
        {
            //用于写入context.HttpContext.Response.Output
            this.View.Render(viewContext,作家);
            writer.Flush();
            的HTML = Encoding.UTF8.GetString(stream.ToArray());
        }
        如果(结果!= NULL)
        {
            result.ViewEngine.ReleaseView(背景下,this.View);
        }
    }
}

这重写基方法的的ExecuteReuslt(这是从基方法,我重写code;可以RC1已经改变)来呈现给我控制,而不是响应输出流的流。因此,翻出它究竟会如何呈现到客户机的文本。

接下来,你将如何在一个控制器动作使用:

 公众的ActionResult CreateEmailView(RegistrationInformation信息)
{
  VAR userInformation = Membership.CreateNewUserLol(信息);
  //抓住我们的普通视图,以便我们可以得到一些信息出来的
  VAR resultView =查看(userInformation);  //创建我们的字符串结果并对其进行配置
  StringResult SR =新StringResult();
  sr.ViewName = resultView.ViewName;
  sr.MasterName = resultView.MasterName;
  sr.ViewData = userInformation;
  sr.TempData = resultView.TempData;
  // 让他们吃蛋糕
  sr.ExecuteResult(this.ControllerContext);
  字符串emailHtml = sr.Html;
  //真棒utils软件包,纨绔子弟
  Utils.SendEmailKThx(userInformation,emailHtml);  返回resultView;
}

我两次呈现同样的看法;第一次我把它渲染的流,我第二次使其正常运行。有可能潜入的ViewResult其他地方的调用链,改变如何渲染工作,但在code粗略地看一眼并没有透露任何。而框架是pretty不错,这个过程的一部分调用堆栈只是没有足够精细度的,可以很容易改变的过程中一个步骤。如果他们分手的ExecuteReuslt成几个不同的重写方法,我们可以从渲染到输出流渲染我们的流,而不覆盖整个的ExecuteReuslt方法改变了它。哦....

I'd like to use my Action in asp.net mvc, as template engine, that results in form of string, that I could send in email.

Pseudo-Code:

public ActionResult Register()
{
    SendEmail(View().ToString());

    return new EmptyResult();
}

解决方案

First, you'll still probably want to return a view from your action, so returning an EmptyResult isn't the best; but you'll figure that out when you're dealing with the page flow in your registration process.

I'm assuming that you wish to create an HTML email using a View you have already created. That means you wish to take the result of something that looks like the following:

public ActionResult CreateEmailView(RegistrationInformation info)
{
  var userInformation = Membership.CreateNewUserLol(info);
  return View(userInformation)
}

and send that as the body of the email. You get to reuse your views and all that fun stuff.

You can take advantage of the framework by creating a custom ActionResult and using this to generate your text.

Here is some c#-like pseudocode that might actually compile and work. First, the custom ActionResult:

public class StringResult : ViewResult
{
    public string Html { get; set; }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (string.IsNullOrEmpty(this.ViewName))
        {
            this.ViewName = 
                 context.RouteData.GetRequiredString("action");
        }
        ViewEngineResult result = null;
        if (this.View == null)
        {
            result = this.FindView(context);
            this.View = result.View;
        }
        ViewContext viewContext = new ViewContext(
                context, this.View, this.ViewData, this.TempData);
        using (var stream = new MemoryStream())
        using (var writer = new StreamWriter(stream))
        {
            // used to write to context.HttpContext.Response.Output
            this.View.Render(viewContext, writer);
            writer.Flush();
            Html = Encoding.UTF8.GetString(stream.ToArray());
        }
        if (result != null)
        {
            result.ViewEngine.ReleaseView(context, this.View);
        }
    }
}

This overrides the base method's ExecuteResult (this is the code from the base method I'm overriding; may have changed in RC1) to render to a stream that I control instead of the Output stream of the Response. So it pulls out the text exactly how it would be rendered to the client machine.

Next, how you would use this in a controller action:

public ActionResult CreateEmailView(RegistrationInformation info)
{
  var userInformation = Membership.CreateNewUserLol(info);
  // grab our normal view so we can get some info out of it
  var resultView = View(userInformation);

  // create our string result and configure it
  StringResult sr = new StringResult();
  sr.ViewName = resultView.ViewName;
  sr.MasterName = resultView.MasterName;
  sr.ViewData = userInformation;
  sr.TempData = resultView.TempData;
  // let them eat cake
  sr.ExecuteResult(this.ControllerContext);
  string emailHtml = sr.Html;
  // awesome utils package, dude
  Utils.SendEmailKThx(userInformation, emailHtml);

  return resultView;
}

I'm rendering the same view twice; the first time I render it to a stream and the second time I render it normally. It might be possible to sneak into the call chain of ViewResult somewhere else and change how Render operates, but a cursory glance at the code doesn't reveal anything. While the framework is pretty good, the call stack for parts of the process are just not fine grained enough to make it easy to change a single step in the process. If they broke ExecuteResult into a few different overridable methods, we could have changed it from rendering to the output stream to rendering to our stream without overriding the entire ExecuteResult method. Oh well....

这篇关于发送电​​子邮件里面asp.net的MVC作用的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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