我正在尝试使用ASP MVC Postal在后台作业中发送电子邮件,如下所示:
public void CommentCreated(Comment comment, ApplicationUser user)
{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new Postal.EmailService(engines);
var email = new CommentCreatedEmail
{
To = user.Email,
From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
Subject = "Comment Created"
Comment = comment,
User = user
};
emailService.Send(email);
}我的视图CommentCreated.cshtml如下:
@model MyApp.Models.CommentCreatedEmail
To: @Model.To
From: @Model.From
Subject: @Model.Subject
<p>
<a href="@Url.Action("Details", "Comment", new { id = @Model.Comment.Id }, "http")">@Url.Action("Details", "Comment", new { id = @Model.Comment.Id }, "http")</a>
但是我得到了以下错误:
Exception thrown: 'RazorEngine.Templating.TemplateCompilationException' in RazorEngine.dll
Exception thrown: 'System.Web.HttpCompileException' in System.Web.dll有什么想法吗?
发布于 2016-08-23 09:27:37
使用异常处理来捕获和识别错误发生的位置
public void CommentCreated(Comment comment, ApplicationUser user)
{
try{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new Postal.EmailService(engines);
var email = new CommentCreatedEmail
{
To = user.Email,
From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
Subject = "Comment Created"
Comment = comment,
User = user
};
emailService.Send(email);
}
catch(TemplateCompilationException ex)
{
foreach (var compilerError in ex.CompilerErrors)
{
Console.WriteLine(string.Format("{0} - {1} - Line {2} Column {3} in {4}", compilerError.ErrorNumber, compilerError.ErrorText, compilerError.Line, compilerError.Column, compilerError.FileName));
}
}
}发布于 2017-02-03 22:58:31
看起来像下面这行:
Subject = "Comment Created"后面少了一个逗号(,),不是吗
Subject = "Comment Created",发布于 2017-04-20 03:02:36
Razor引擎在MVC世界之外有一些限制,因此,许多与它相关的东西根本不能工作。
我最近遇到了这个问题,我的“解决方案”是在应用配置中设置重定向基URL,并在运行时组合它,类似于:
var email = new CommentCreatedEmail
{
To = user.Email,
From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
Subject = "Comment Created"
Comment = comment,
User = user
CommentLink = ConfigurationManager.AppSettings["SiteUrl"] + "Comment/Details/"+comment.Id
};在你的.cshtml文件中:
@model MyApp.Models.CommentCreatedEmail
To: @Model.To
From: @Model.From
Subject: @Model.Subject
<p>
<a href="@Model.CommentLink">Comment</a>希望它能帮上忙!
https://stackoverflow.com/questions/39086731
复制相似问题