簡體   English   中英

剃刀HTML到使用石英字符串

[英]razor html to string using Quartz

我一直在嘗試獲取html並將剃刀代碼轉換為字符串以發送並通過電子郵件發送給多個用戶。 電子郵件是由Quartz安排的,並發送給用戶。

郵件正在通過@Url.Action生成鏈接。 我注意到我的應用程序此時沒有ControllerHttpContext 我一直在嘗試將razor代碼( RazorEngineMvcMailer )轉換為字符串並發送電子郵件的方法,但是沒有用,因為我無法轉換@Url.Action並且找不到MvcMailer於Visual Studio的MvcMailer工作包2017年

有沒有辦法做到這一點?

這是電子郵件的模板:

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

正如@Stanislav Nedeljkovic所建議的那樣,我將Url放在配置文件中,並設法用它創建HttpContext 然后,我可以用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