简体   繁体   English

剃刀HTML到使用石英字符串

[英]razor html to string using Quartz

I have been trying to obtain a html and translate the razor code in a string to send and email to multiple users. 我一直在尝试获取html并将剃刀代码转换为字符串以发送并通过电子邮件发送给多个用户。 The emails are scheduled by Quartz and send to the users. 电子邮件是由Quartz安排的,并发送给用户。

The mail is generating a link via @Url.Action . 邮件正在通过@Url.Action生成链接。 I notice that I don't have a Controller nor HttpContext at this point on my application. 我注意到我的应用程序此时没有ControllerHttpContext I have been trying a way to translate the razor code ( RazorEngine and MvcMailer ) to a string and sending in the email, but with no use because I cannot translate the @Url.Action and can't find a working package MvcMailer for Visual Studio 2017 我一直在尝试将razor代码( RazorEngineMvcMailer )转换为字符串并发送电子邮件的方法,但是没有用,因为我无法转换@Url.Action并且找不到MvcMailer于Visual Studio的MvcMailer工作包2017年

Is there a way to possible do this? 有没有办法做到这一点?

Here is the template of the Email: 这是电子邮件的模板:

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

Here is the email generator 这是电子邮件生成器

public MailMessage GetMessage(EmailModel notification)
{

    string BodyTemplate = ReplaceEmailBody(notification);

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

Here is my rubbish attempt to replace the razor: 这是我尝试更换剃刀的垃圾:

    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
    }

All this code is running in a execute job of Quartz 所有这些代码都在Quartz的执行作业中运行

I'm using Visual Studio 2017 我正在使用Visual Studio 2017

As @Stanislav Nedeljkovic suggested, I put the Url on a config file and manage to create and HttpContext with it. 正如@Stanislav Nedeljkovic所建议的那样,我将Url放在配置文件中,并设法用它创建HttpContext Then I could translate the rest with RazorEngine . 然后,我可以用RazorEngine翻译其余的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});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM