razor html 使用 Quartz 串起来
razor html to string using Quartz
我一直在尝试获取 html 并将剃刀代码翻译成字符串,以便发送和通过电子邮件发送给多个用户。电子邮件由 Quartz
安排并发送给用户。
邮件正在通过 @Url.Action
生成 link。我注意到此时我的申请中没有 Controller
或 HttpContext
。我一直在尝试将 razor
代码(RazorEngine
和 MvcMailer
)翻译成字符串并发送到电子邮件中,但没有用,因为我无法翻译 @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
正如@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});
我一直在尝试获取 html 并将剃刀代码翻译成字符串,以便发送和通过电子邮件发送给多个用户。电子邮件由 Quartz
安排并发送给用户。
邮件正在通过 @Url.Action
生成 link。我注意到此时我的申请中没有 Controller
或 HttpContext
。我一直在尝试将 razor
代码(RazorEngine
和 MvcMailer
)翻译成字符串并发送到电子邮件中,但没有用,因为我无法翻译 @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
正如@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});