【问题标题】:Rendering HTML content with @Html.Raw in Razor Mail Template在 Razor 邮件模板中使用 @Html.Raw 呈现 HTML 内容
【发布时间】:2017-01-09 23:24:32
【问题描述】:

使用 Razor 页面作为邮件模板我正在尝试使用 @Html.Raw(Model.Content) 显示邮件的内容(Html 内容)。

每当我运行代码时,我都会收到此错误:html does not exist in current context

我在另一个剃须刀页面上尝试了这个@Html.Raw("<strong>Bold!</strong>"),以验证RazorEngine 已安装并且它显示得非常好,没有错误。

【问题讨论】:

标签: html asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

在处理电子邮件时,我使用 RazorEngine.Templating 中的 RazorEngineService,例如就我而言,它看起来像这样:

using RazorEngine.Templating;

RazorEngineService.Create().RunCompile(html, ...) 

假设您使用的是相同的程序集,@Html.Raw 确实 NOT 存在这种用法。通过在我的电子邮件中执行此操作,我终于能够获得原始 HTML 输出:

@using RazorEngine.Text

@(new RawString(Model.Variable))

【讨论】:

  • 我在什么时候使用RazorEngineService.Create().RunCompile(...)
  • @uikrosoft,很抱歉造成混乱。如果您没有使用 RazorEngine.Templating,那么您将看不到这个。您没有发布用于将 .cshtml 文件转换为电子邮件的代码。这就是为什么我只是让你看看我在做什么并给出一种可能性。您可以尝试将@using RazorEngine.Text 添加到您的电子邮件CSHTML 页面,然后将您的HTML 包装在@(new RawString(Model.Variable)) 中,看看它是否有效。如果您加载了相同的程序集,那应该可以工作。
  • 救命稻草。我永远不会想到“使用”。没有它,它就不起作用。
【解决方案2】:
@using RazorEngine.Text

@(new RawString(Model.Variable))

在邮件模板 cshtml 视图中使用此代码。

【讨论】:

【解决方案3】:

我的回答,使用来自here 的hannes neukermans 回答。

我需要在 MVC 项目中使用 RazorEngine 来发送包含存储在数据库中的 html 字符串的电子邮件,以便管理员用户可以编辑它们。

标准的 RazorEngine 配置不允许 @Html.Raw 工作。

在我的电子邮件类中,我设置了一个新的 Engine.Razor(引擎是静态的),其中包含 Hannes 推荐的类。我只需要 Raw 方法,但您显然可以添加其他方法:

你需要这些:

 using RazorEngine;
 using RazorEngine.Templating; // For extension methods.
 using RazorEngine.Text;
 using RazorEngine.Configuration;

这些是提供@Html 帮助器的类

public class HtmlSupportTemplateBase<T> : TemplateBase<T>
{
    public HtmlSupportTemplateBase()
    {
        Html = new MyHtmlHelper();
    }
    public MyHtmlHelper Html { get; set; }
}  

 public class MyHtmlHelper
{
    /// <summary>
    /// Instructs razor to render a string without applying html encoding.
    /// </summary>
    /// <param name="htmlString"></param>
    /// <returns></returns>
    public IEncodedString Raw(string htmlString)
    {
        return new RawString(WebUtility.HtmlEncode(htmlString)); //you may not need the WebUtility.HtmlEncode here, but I did
    } 
}

然后我可以在我的电子邮件模板中使用 @Html.Raw 来合并可编辑的 html

public class Emails
{
    public static TemplateServiceConfiguration config 
                = new TemplateServiceConfiguration(); // create a new config

    public Emails()
    {   
        config.BaseTemplateType = typeof(HtmlSupportTemplateBase<>);// incorporate the Html helper class
        Engine.Razor = RazorEngineService.Create(config);// use that config to assign a new razor service
    }

    public static void SendHtmlEmail(string template,  EmailModel model)
    {           
        string emailBody 
             = Engine.Razor.RunCompile(template, model.Type.ToString(), typeof(EmailModel), model); 

        var smtpClient = getStaticSmtpObject(); // an external method not included here     
        MailMessage message = new MailMessage();
        message.From = new MailAddress(model.FromAddress);
        message.To.Add(model.EmailAddress); 
        message.Subject = model.Subject;
        message.IsBodyHtml = true;
        message.Body =  System.Net.WebUtility.HtmlDecode(emailBody);
        smtpClient.SendAsync(message, model); 
    }
}

然后我可以通过传入从实际 .cshtml 模板中读取的字符串和保存电子邮件数据的模型来使用它。

string template = System.IO.File.ReadAllText(ResolveConfigurationPath("~/Views/Emails/Email.cshtml"));
SendHtmlEmail(template, model);

ResolveConfigurationPath 是我找到的另一个外部函数。

 public static string ResolveConfigurationPath(string configPath)
    {
        if (configPath.StartsWith("~/"))
        {
            // +1 to Stack Overflow:
            // http://stackoverflow.com/questions/4742257/how-to-use-server-mappath-when-httpcontext-current-is-nothing

            // Support unit testing scenario where hosting environment is not initialized.
            var hostingRoot = HostingEnvironment.IsHosted
                              ? HostingEnvironment.MapPath("~/")
                              : AppDomain.CurrentDomain.BaseDirectory;

            return Path.Combine(hostingRoot, configPath.Substring(2).Replace('/', '\\'));
        }
        return configPath;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2016-05-14
    • 2022-01-24
    相关资源
    最近更新 更多