【问题标题】:razor html to string using Quartzrazor html 使用 Quartz 转换成字符串
【发布时间】:2017-10-10 14:16:18
【问题描述】:

我一直在尝试获取 html 并将剃须刀代码转换为字符串以发送和电子邮件给多个用户。电子邮件由Quartz 安排并发送给用户。

邮件正在通过@Url.Action 生成链接。我注意到我的应用程序此时没有ControllerHttpContext。我一直在尝试一种将razor 代码(RazorEngineMvcMailer)转换为字符串并在电子邮件中发送的方法,但没有用,因为我无法翻译@Url.Action 并且找不到工作用于 Visual Studio 2017 的包 MvcMailer

有没有办法做到这一点?

这是电子邮件的模板:

Hi @ViewBag.RecipientName,

Client that buy this item is @Model.ClientName 
<p> <a href='@Url.Action("Index", "Item", new { ItemId = Model.ItemId}, Request.Url.Scheme)'>Click here to check the Item</a> </p>

@ViewBag.RecipientEmailAddress

这是电子邮件生成器

public MailMessage GetMessage(EmailModel notification)
{

    string BodyTemplate = ReplaceEmailBody(notification);

    return new MailMessage
    {
        To = { "testuser@testdomain.com" },
        Subject = DateTime.Now.ToString(),
        IsBodyHtml = true,
        Body = BodyTemplate
    };
}

这是我更换剃须刀的垃圾尝试:

    private string ReplaceEmailBody(EmailModel notification)
    {
        string notificationBody = "";

        notificationBody = Templates.Resources.MailTemplate;

        notificationBody = notificationBody.Replace("@ViewBag.RecipientName", notification.RecipientName);
        notificationBody = notificationBody.Replace("@ViewBag.RecipientEmailAddress", notification.RecipientEmailAddress);
        notificationBody = notificationBody.Replace("@Model.CLIENT_NAME", notification.ClientName);

        //Need to replace the Url.Action
    }

所有这些代码都在Quartz的执行作业中运行

我正在使用 Visual Studio 2017

【问题讨论】:

  • 您可以向您的石英服务提供 url(mvc 控制器操作),该服务将被调用以生成剃刀电子邮件(并最终发送)。
  • @StanislavNedeljkovic 我该怎么做?
  • 如果 url 是静态的,把它放在配置文件中,如果是动态的,把它作为作业参数发送。

标签: c# asp.net-mvc email razor quartz.net


【解决方案1】:

正如@Stanislav Nedeljkovic 建议的那样,我将Url 放在配置文件中并设法创建HttpContext。然后我可以用RazorEngine 翻译其余部分。

        var request = new HttpRequest("/", ConfigFile.Url, "");
        var response = new HttpResponse(new StringWriter());
        httpContext = new HttpContext(request, response);


        var httpContextBase = new HttpContextWrapper(httpContext);
        var routeData = new RouteData();
        var requestContext = new RequestContext(httpContextBase, routeData);

        var urlHelper = new UrlHelper(requestContext);
        var url = urlHelper.Action("Index", "Item", new { ItemId = model.itemId});

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多