最近,我一直在使用Antaris RazorEngine为我正在进行的C#类库做一些电子邮件模板。到目前为止,我可以从模型中获得一般文本显示在视图上。但是当我试图显示一个图像引擎抛出异常时:
类型' RazorEngine.Templating.TemplateCompilationException'的第一次机会异常发生在RazorEngine.dll
目前我有一个UserModel类,它包含3个属性,这些属性具有字符串数据,另一个属性具有从Resources.resx文件加载的Bitmap图像。我想知道来自Antaris的第三方RazorEngine不支持以位图格式渲染图像吗?我似乎无法找到与此相关的任何详细文档: - (
以下相关代码:
Email_Template.cshtml
@Model MVC_Util_Naqi.UserModel
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<img src="@Model.LogoImage" alt="" width="500" height="250" />
</head>
<body>
<title>Title Goes here</title>
<p> Hi @Model.Name,</p>
<p>Thanks for signing up to <a href="http://awesomesite.com">our awesome site</a>!</p>
@if (!Model.IsPremiumUser) {
<p>To make the most of it, <a href="http://awesomesite.com/upgrade">upgrade to our premium package now</a>!</p>
}
<p>John, Founder</p>
</body>
</html>
SMTP_eMail.cs文件中的SMTP电子邮件方法
public void ReadyTheEmail(){
this.SMTP_HostName = "---------";
this.SMTP_HostPort = -----;
this.SMTP_Username = "-----";
this.SMTP_Password = "-------";
this.eMailFromAddress = "---------";
this.eMailToAddress = "---------------";
this.eMailSubject = "Test - C# Email Functionality";
this.eMailHTMLBody = "";
this.IsSSLEnabled = true;
try{
if (TestConnection(SMTP_HostName, SMTP_HostPort)){
smtpServer.Host = SMTP_HostName;
smtpServer.Credentials = new System.Net.NetworkCredential(SMTP_Username, SMTP_Password);
smtpServer.EnableSsl = IsSSLEnabled;
mail.From = new MailAddress(eMailFromAddress);
mail.To.Add(eMailToAddress);
mail.Subject = eMailSubject;
mail.IsBodyHtml = true;
eMailHTMLBody = "<h1> HTML Body goes here :-) </h1>";
StringBuilder sb = new StringBuilder(eMailHTMLBody);
System.Reflection.Assembly myAssembly;
myAssembly = this.GetType().Assembly;
System.Resources.ResourceManager myRManager = new System.Resources.ResourceManager("MVC_Util_Naqi.Properties.Resources", myAssembly);
byte[] file = (byte[])myRManager.GetObject("Email_Template");
MemoryStream ms = new MemoryStream(file);
StreamReader sReader = new StreamReader(ms);
string template = sReader.ReadToEnd();
//string template_final = RemoveInheritsDirective(template);
UserModel model = new UserModel() { Name = "Ali", Email = "Ali@gmail.com", IsPremiumUser = true };
string template1 = @"@Model MVC_Util_Naqi.UserModel
Hello @Model.Name, welcome to RazorEngine!
Hello @Model.Email, welcome to RazorEngine!
Hello @Model.IsPremiumUser, welcome to RazorEngine!";
//var result = Razor.Parse(template, model, "template1");
Razor.Compile(template, typeof(UserModel), "compiled-tempalte");
string result = Razor.Run(model, "compiled-tempalte");
sb.Append(result);
//ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
//byte[] file = (byte[]) resourceSet.GetObject("PDF_eMail.cshmtl");
//MemoryStream ms = new MemoryStream(file);
//foreach (DictionaryEntry entry in resourceSet) {
//System.Diagnostics.Debug.WriteLine(entry.Key.ToString());
//object resource = entry.Value;
//System.Diagnostics.Debug.WriteLine(entry.Value.GetType());
//}
mail.Body = sb.ToString();
}
else{
System.Diagnostics.Debug.WriteLine("Connection Problem");
}
}
catch (Exception ex){
Console.WriteLine(ex.ToString());
}
}