RazorEngine "Unable to compile template" Error
Asked Answered
B

2

1

I'm using the following code to build and send an email using RazorEngine.

//build email using template.
string template = File.OpenText(EmailTemplatePath).ReadToEnd();
OrganizationInviteEmailTemplate model = new OrganizationInviteEmailTemplate()
{
    FirstName = Invitation.FirstName,
    LastName = Invitation.LastName,
    Message = Message,
    OrganizationName = Invitation.Organization.OrganizationName,
    ConfirmUrl = string.Format(ConfirmUrlTemplate, Invitation.InviteCode)
};
string body = Razor.Parse(template, model);

//email the invitation.
MailMessage message = new MailMessage();
message.To.Add(Invitation.Email);
message.Subject = "Invitation From " + Invitation.Organization.OrganizationName;
message.Body = body;
message.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Send(message);

The template is below:

 <html>
     <head>
     </head>
     <body>
         <h3>@Model.OrganizationName has invited you to be part of their organization</h3>

         <p>
         Hi @Model.FirstName,
         </p>

         .....etc......
     </body>
 </html>

I get the "Unable to compile template" error 9 times out of 10 and then it will work once after I adjust the template by removing Model. and/or adjusting the code to use Parse instead, but then it will go right back to not working. It's like something is caching a piece of code briefly or something.

I'm using the RazorEngine.dll included with RazorJS, version 2.1.4113.149. Maybe that's the issue. Going to try this library or Postal next since this is taking way too much time already. Or just hard code the damn thing.

Briones answered 22/4, 2011 at 14:35 Comment(6)
Take a look onto the RazorEngine discussions on codeplex: razorengine.codeplex.com/discussions/248593.Campagna
Just to clarify, is your template built correctly? meaning all html tags have closing tags when appropriate? the post here doesn't show the closing <h3> tag so that might be causing it to fail - nevermind someone edited your post weirdlyGarrity
I created a quick OrganizationInviteEmailTemplate model, populated it and ran it with your snipped and it worked fine. Email me ben@ this name.com with a copy of your model and the full template and I'll be able to look and see where the error might be.Garrity
Well thanks for looking. It must have something to do with other assemblies or that it's executing in IIS. I don't think we're going to solve this, so just going to hard code it and use string.Format. I've spent way too much time already trying to get this to work.Briones
No problemo. I can tell you this: I use the RazorEngine in a production web app on an IIS7 server as well as in a windows application here at my work. I've come across a few bugs but nothing that prevents it from working outright with a few tweaks to the templates.Garrity
Just an FYI, I removed RazorJS and the problem when away. My guess would be RazorJS is reconfiguring some globals and causing the issue.Briones
C
-1

Unless you want to code it all by yourself, it will be more easy to install a nuget package like MvcMailer.

Chericheria answered 22/4, 2011 at 15:1 Comment(0)
P
-1

Aren't you missing the @model declaration at the top of your template?

Photoconductivity answered 16/8, 2011 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.