我有使用RazorEngine生成电子邮件的MVC5应用程序。每封电子邮件都在cshtml模板中。模板将数据传递给他们,因此即使我这样做,缓存它们并不会真正有用。还有什么我可以做的,以减少渲染所需的时间吗?在Azure服务器上花几秒钟来渲染。
@{
ViewBag.Title = "_EmailResetPassword";
}
@{ var PasswordToken = Model.token; }
@{ var URLpath = Model.URLpath; }
@{ string link = string.Format("http://{0}/Account/ResetPassword/?tokenid={1}", URLpath, PasswordToken); }
<html>
<head>
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
.auto-style2 {
background: white;
text-align: left;
font-size: 11.0pt;
font-family: Helvetica, sans-serif;
color: #4D4D4D;
}
</style>
</head>
<body>
<div class="auto-style1">
<h3 class="auto-style2">Dear @Model.FirstName,</h3>
<h3 class="auto-style2">We have reset your password at Eph Apparel for the username: @Model.UserName</h3>
<h3 class="auto-style2">Please click on the following link to create a new password:</h3>
<h3 style='text-align:center;background:white'><span lang=EN-CA
style='font-size:11.0pt;font-family:"Helvetica","sans-serif"'>
<a href="@link">Reset Password</a></span></h3>
<h3 style='text-align:center;background:white'><span lang=EN-CA
style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";color:#4D4D4D'>LIKE US ON </span>
<span> <a href="https://www.facebook.com/ephapparel" target="_blank"><img alt="" width=25 height=22
src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/fb_icon.png" ></a></span>
<span lang=EN-CA style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";
color:#4D4D4D'> FOLLOW US ON </span><span lang=EN-CA></span>
<span><a href="https://www.twitter.com/ephApparel" target="_blank"><img alt="" width=25 height=23
src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/twitter_icon.png"></a></span></h3>
<br>
<img alt="" src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/eph_logo.png">
</div>
</body>
</html>
代码背后:
private string ResetPassword(Customer oCustomer, string oToken)
{
string oURLpath = GetConfigSettingById(3);
string template = System.IO.File.ReadAllText(HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml"));
string message = Razor.Parse(template, new { FirstName = oCustomer.FirstName, UserName = oCustomer.Email, token = oToken, URLpath = oURLpath }, "Reset");
return message;
}
答案 0 :(得分:0)
private string ResetPassword(Customer oCustomer, string oToken)
{
string oURLpath = GetConfigSettingById(3);
string template = System.IO.File.ReadAllText(HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml"));
string message = Razor.Parse(template, new { FirstName = oCustomer.FirstName, UserName = oCustomer.Email, token = oToken, URLpath = oURLpath });
return message;
}
我要走出困境并说这是你的减速发生的地方......这些类型的操作在网站上非常昂贵,特别是如果它在别处运行(I.E. Azure)。此操作正在执行的操作是逐字加载整个文件,然后在每次重置密码时填充它。
您可以使用电子邮件进行3种选择。
要对其进行模板化,请执行以下操作:
首先,将Razor Elements变成一个新类。 IE:
public class ResetPassword
{
public string FirstName { get; set; }
public string Email { get; set; }
public string Token { get; set; }
public string UrlPath { get; set; }
}
然后更改您的html以使用新类作为其模型:
@model Your.Namespace.ResetPassword
@{
ViewBag.Title = "_EmailResetPassword";
string link = string.Format("http://{0}/Account/ResetPassword/?tokenid={1}", Model.URLpath, Model.token);
}
<html>
<head>
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
.auto-style2 {
background: white;
text-align: left;
font-size: 11.0pt;
font-family: Helvetica, sans-serif;
color: #4D4D4D;
}
</style>
</head>
<body>
<div class="auto-style1">
<h3 class="auto-style2">Dear @Model.FirstName,</h3>
<h3 class="auto-style2">We have reset your password at Eph Apparel for the username: @Model.UserName</h3>
<h3 class="auto-style2">Please click on the following link to create a new password:</h3>
<h3 style='text-align:center;background:white'><span lang=EN-CA
style='font-size:11.0pt;font-family:"Helvetica","sans-serif"'>
<a href="@link">Reset Password</a></span></h3>
<h3 style='text-align:center;background:white'><span lang=EN-CA
style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";color:#4D4D4D'>LIKE US ON </span>
<span> <a href="https://www.facebook.com/ephapparel" target="_blank"><img alt="" width=25 height=22
src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/fb_icon.png" ></a></span>
<span lang=EN-CA style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";
color:#4D4D4D'> FOLLOW US ON </span><span lang=EN-CA></span>
<span><a href="https://www.twitter.com/ephApparel" target="_blank"><img alt="" width=25 height=23
src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/twitter_icon.png"></a></span></h3>
<br>
<img alt="" src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/eph_logo.png">
</div>
</body>
最后,回到您的代码,尝试以下方法:
private string ResetPassword(Customer oCustomer, string oToken)
{
string template = HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml");
ResetPassword password = new ResetPassword
{
FirstName = oCustomer.FirstName,
UserName = oCustomer.Email,
token = oToken,
URLpath = GetConfigSettingById(3)
};
var templateService = new TemplateService();
return templateService.Parse(File.ReadAllText(template), password, null, "Reset");
}
尝试使用此功能或第三方API,并告知我们结果! (如果您需要有关Postal的帮助,请在上面提出新问题,我会很乐意提供一些意见)