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

エラーが発生している場所をキャッチして特定する例外処理を組み込む

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 に答える