我可以用一个ASP.Net MVC的Razor视图生成格式良好的HTML身体为输入从服务器发送的电子邮件? [英] Can I use an ASP.Net MVC Razor view to generate an nicely formatted HTML Body as the input for an email sent from the server?

查看:1206
本文介绍了我可以用一个ASP.Net MVC的Razor视图生成格式良好的HTML身体为输入从服务器发送的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用模型绑定/渲染的剃刀视图功能生成的电子邮件,我从我的ASP.NET MVC应用程序发送HTML正文内容。

有没有一种方法来呈现,而不是返回它作为一个GET请求的的ActionResult的观​​点为一个字符串?

要说明我正在寻找的东西,将执行下列操作...

 公众的ActionResult SendEmail(INT ID)
    {
        。EmailDetailsViewModel emailDetails = EmailDetailsViewModel()CreateEmailDetails(ID);        //这是我需要帮助...
        //我想我的视图模型(emailDetails)传递给我的视图(EmailBodyRazorView),但不是人丁的响应流我想捕捉的输出,并把它传递给电子邮件客户端。
        字符串htmlEmailBody =视图(EmailBodyRazorView,emailDetails)的ToString();        //一旦我有htmlEmail身体,我好去。我有一个utilityt将发送电子邮件给我。
        MyEmailUtility.SmtpSendEmail(stevejobs@apple.com,电子邮件主题,htmlEmailBody);        //重定向另一个动作,将一个页面返回给用户确认邮件已发出。
        返回RedirectToAction(ConfirmationEmailWasSent);
    }


解决方案

如果你只需要渲染视图转换成字符串尝试是这样的:

 公共字符串ToHtml(字符串viewToRender,可视数据的ViewDataDictionary,ControllerContext controllerContext)
{
    VAR的结果= ViewEngines.Engines.FindView(controllerContext,viewToRender,NULL);    StringWriter的输出;
    使用(输出=新的StringWriter())
    {
        VAR viewContext =新ViewContext(controllerContext,result.View,可视数据,controllerContext.Controller.TempData,输出);
        result.View.Render(viewContext,输出);
        result.ViewEngine.ReleaseView(controllerContext,result.View);
    }    返回output.ToString();
}

您需要在视图和控制器行动的ViewData和ControllerContext的名字来传递。

I'd like to make use of the Model Binding / Rendering capabilities of a Razor View to generate the HTML Body Content for an email I'm sending from my ASP.NET MVC Application.

Is there a way to render a view to a string instead of returning it as the ActionResult of a GET request?

To illustrate I'm looking for something that will do the following...

    public ActionResult SendEmail(int id)
    {
        EmailDetailsViewModel emailDetails = EmailDetailsViewModel().CreateEmailDetails(id);

        // THIS IS WHERE I NEED HELP...
        // I want to pass my ViewModel (emailDetails) to my View (EmailBodyRazorView) but instead of Rending that to the Response stream I want to capture the output and pass it to an email client.
        string htmlEmailBody = View("EmailBodyRazorView", emailDetails).ToString();

        // Once I have the htmlEmail body I'm good to go.  I've got a utilityt that will send the email for me.
        MyEmailUtility.SmtpSendEmail("stevejobs@apple.com", "Email Subject", htmlEmailBody);

        // Redirect another Action that will return a page to the user confirming the email was sent.
        return RedirectToAction("ConfirmationEmailWasSent");
    }

解决方案

If you just need to render the view into a string try something like this:

public string ToHtml(string viewToRender, ViewDataDictionary viewData, ControllerContext controllerContext)
{
    var result = ViewEngines.Engines.FindView(controllerContext, viewToRender, null);

    StringWriter output;
    using (output = new StringWriter())
    {
        var viewContext = new ViewContext(controllerContext, result.View, viewData, controllerContext.Controller.TempData, output);
        result.View.Render(viewContext, output);
        result.ViewEngine.ReleaseView(controllerContext, result.View);
    }

    return output.ToString();
}

You'll need to pass in the name of the view and the ViewData and ControllerContext from your controller action.

这篇关于我可以用一个ASP.Net MVC的Razor视图生成格式良好的HTML身体为输入从服务器发送的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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