0

我正在尝试在后台作业中使用 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

有任何想法吗

4

3 回答 3

0

看起来像这条线:

Subject = "Comment Created"

后面少了一个逗号(,),不应该吗

Subject = "Comment Created",
于 2017-02-03T14:58:30.597 回答
0

进行异常处理以捕获并识别错误发生的位置

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));
        }
    }
}
于 2016-08-23T01:27:36.687 回答
0

Razor Engine 在 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>

希望能帮助到你!

于 2017-04-19T19:02:36.103 回答