2015-11-01 58 views
0

我有一個確認郵件模板.cshtml如下:設置「CSHTML」數據並返回一個字符串

<html> 
    <head> 
     <title>company name: Email Verification - Action Required</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
      Thanks for signing up for company name! 
      <br/> 
      <br/> 

      Thank you for signing up to company name. Please press the link below to confirm your email address <br/> 
      If you did not sign up for company name, please ignore this email, and accept our apologies for the inconvenience. 

      <br/> 
      <br/> 

      <a href="@Model.ConfirmLink">@Model.ConfirmLink</a> 

      <br/> 
      <br/> 
      <br/> 

      Thanks,<br/> 
      The <a href="http://company name.com" >company name</a> Team      
     </table> 
    </body> 
</html> 

是否有一個機制,我可以設置數據(@Model.ConfirmLink),並得到一個結果作爲string? (不是手動替換字符串)

回答

1

您可以使用RazorEngine,它使用與Razor相同的語法,但可讓您在無需ASP.NET MVC的情況下呈現文本。

如何使你的代碼將工作:

string template = yourHtmlCodeAsString; 
string result = Razor.Parse(template, new { ConfirmLink = "http://.../" }); 

您可以從一個文件,資源加載模板串等

0

是的,您可以使用Razor作爲電子郵件的模板引擎。

我使用這個類。

public static class EmailFactory 
{ 
    public static string ParseTemplate<T>(T model, string templatesPath, EmailType emailType) 
    { 
     string templatePath = Path.Combine(templatesPath, string.Format("{0}.cshtml", emailType)); 
     string content = templatePath.ReadTemplateContent(); 

     return Razor.Parse(content, model); 
    } 
} 
  • templatesPath - 是我的電子郵件CSHTML意見的路徑。
  • EmailType是和enum的元素與templatesPath目錄中的view namse相同。

本質是Razor.Parse(content, model);它需要一個字符串,它是一個有效的剃刀視圖和模型,並將它們合併在一起。與ASP MVC中的視圖相同。

相關問題