首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RazorEngine慢

RazorEngine慢
EN

Stack Overflow用户
提问于 2014-06-26 18:47:11
回答 1查看 1.7K关注 0票数 0

我有MVC5应用程序,它使用RazorEngine生成电子邮件。每封电子邮件都在cshtml模板中。模板中有数据被传递给它们,所以即使我这样做,缓存它们也没有真正的帮助。我还能做些什么来减少渲染时间吗?在Azure服务器上呈现需要几秒钟。

代码语言:javascript
复制
@{
    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>
        &nbsp;<span lang=EN-CA style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";
        color:#4D4D4D'>&nbsp;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>

代码背后:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-27 13:09:07

代码语言:javascript
复制
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;
}

我要冒险说这就是你减速的地方.这类操作在站点上非常昂贵,特别是在其他地方运行时(即Azure)。此操作实际上是加载整个文件,然后每次重置密码时都填充它。

电子邮件一般有3种选择。

  1. 把它留在你现在拥有的地方(它能工作,但速度慢)
  2. 采用模板/缓存方法(我将在几秒钟内将其放入其中,在启动时会导致显着的减速,但在运行时它会比这个好得多)
  3. 尝试第三方api (当我在上一个站点上做一些类似的事情时,我发现邮政在速度方面相当不错)。

为了模板它,请执行以下操作:

  1. 首先,将Razor元素变成一个新的类。IE: 公共类ResetPassword {公共字符串FirstName { get;set;}公共字符串电子邮件{ get;set;}公共字符串令牌{ get;set;}公共字符串UrlPath { get;set;}
  2. 然后更改html以使用新类作为其模型: @model Your.Namespace.ResetPassword @{ ViewBag.Title = "_EmailResetPassword";string link = string.Format("http://{0}/Account/ResetPassword/?tokenid={1}",Model.URLpath,Model.token);} .auto-style1 {文本对齐:中间;} .auto-style2 {背景:白色;文本对齐:左;字体大小: 11.0pt;字体-家庭: Helvetica,sans-serif;颜色:#4D4D4D;}亲爱的@Model.FirstName,我们已经在Eph服装上重置您的密码以获取用户名:@Model.UserName请单击下面的链接创建一个新的密码:重置密码,就像US on US
  3. 最后,回到您的代码,尝试以下内容: 私有字符串ResetPassword(Customer oCustomer,string oToken) { string =新ResetPassword { FirstName = oCustomer.FirstName,UserName = oCustomer.Email,token = oToken,URLpath = GetConfigSettingById(3) };var templateService = new ();返回templateService.Parse(File.ReadAllText(template),密码,null,“重置”);}

给这个,或第三方api一个尝试,并让我们知道结果!(如果你在邮政方面需要帮助,问一个新的问题,我很乐意提供一些意见)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24437951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档