Asp.net mvc RazorEngine速度很慢

Asp.net mvc RazorEngine速度很慢,asp.net-mvc,razorengine,Asp.net Mvc,Razorengine,我有一个MVC5应用程序,它使用RazorEngine生成电子邮件。每封电子邮件都在cshtml模板中。模板有数据传递给它们,所以缓存它们并没有真正的帮助,即使我正在这样做。我还可以做些什么来减少渲染所需的时间?在Azure服务器上渲染需要几秒钟 @{ ViewBag.Title = "_EmailResetPassword"; } @{ var PasswordToken = Model.token; } @{ var URLpath = Model.URLpath; } @{ st

我有一个MVC5应用程序,它使用RazorEngine生成电子邮件。每封电子邮件都在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>
        &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>
我要冒险说这就是你减速的地方。。。在一个站点上,这类操作的成本相当高,尤其是在其他地方(如Azure)运行时。这个操作实际上是加载整个文件,然后在每次重置密码时填充它

对于电子邮件,通常有三种选择

  • 让它保持现在的状态(它可以工作,但速度很慢)
  • 以模板/缓存方法为例(我将在几秒钟内使用该方法,并将在启动时导致显著的减速,但在运行时会比这要好得多)
  • 尝试一个第三方api(当我在上一个站点上做了类似的事情时,我发现在速度方面非常好)
  • 要对其进行模板化,请执行以下操作:

      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");
      }
    
  • 首先,将Razor元素生成一个新类。即:

    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>
        &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>
    

  • 试试这个,或者第三方api,让我们知道结果!(如果您需要邮政方面的帮助,请提出一个新问题,我很乐意为您提供一些帮助)

    RazorEngine可能会很慢,这取决于您正在做什么。。。。我们可以看一些代码来帮助评估吗?除了另外两个非常合适的问题外,您是否使用Visual Studio中的调试在本地运行此程序?如果你是的话,当地发展过程中的“慢”与任何事情都没有关系。性能测试的时间是在生产就绪的服务器上。这是在Azure上的生产服务器上运行的。渲染视图需要几秒钟。
    @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>
        &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>
    
      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");
      }