asp.net-mvc-4razorengine

RazorEngine Error trying to send email


I have an MVC 4 application that sends out multiple emails. For example, I have an email template for submitting an order, a template for cancelling an order, etc...

I have an Email Service with multiple methods. My controller calls the Send method which looks like this:

public virtual void Send(List<string> recipients, string subject, string template, object data)
{
    ...
    string html = GetContent(template, data);
    ...
}

The Send method calls GetContent, which is the method causing the problem:

private string GetContent(string template, object data)
{
    string path = Path.Combine(BaseTemplatePath, string.Format("{0}{1}", template, ".html.cshtml"));
    string content = File.ReadAllText(path);
    return Engine.Razor.RunCompile(content, "htmlTemplate", null, data);
}

I am receiving the error:

The same key was already used for another template!

In my GetContent method should I add a new parameter for the TemplateKey and use that variable instead of always using htmlTemplate? Then the new order email template could have newOrderKey and CancelOrderKey for the email template being used to cancel an order?


Solution

  • Explanation

    This happens because you use the same template key ("htmlTemplate") for multiple different templates. Note that the way you currently have implemented GetContent you will run into multiple problems:

    Solution:

    Implement the ITemplateManager interface to manage your templates:

    public class MyTemplateManager : ITemplateManager
    {
        private readonly string baseTemplatePath;
        public MyTemplateManager(string basePath) {
          baseTemplatePath = basePath;
        }
    
        public ITemplateSource Resolve(ITemplateKey key)
        {
            string template = key.Name;
            string path = Path.Combine(baseTemplatePath, string.Format("{0}{1}", template, ".html.cshtml"));
            string content = File.ReadAllText(path);
            return new LoadedTemplateSource(content, path);
        }
    
        public ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context)
        {
            return new NameOnlyTemplateKey(name, resolveType, context);
        }
    
        public void AddDynamic(ITemplateKey key, ITemplateSource source)
        {
            throw new NotImplementedException("dynamic templates are not supported!");
        }
    }
    

    Setup on startup:

    var config = new TemplateServiceConfiguration();
    config.Debug = true;
    config.TemplateManager = new MyTemplateManager(BaseTemplatePath); 
    Engine.Razor = RazorEngineService.Create(config);
    

    And use it:

    // You don't really need this method anymore.
    private string GetContent(string template, object data)
    {
        return Engine.Razor.RunCompile(template, null, data);
    }
    

    RazorEngine will now fix all the problems mentioned above internally. Notice how it is perfectly fine to use the name of the template as key, if in your scenario the name is all you need to identify a template (otherwise you cannot use NameOnlyTemplateKey and need to provide your own implementation).

    Hope this helps. (Disclaimer: Contributor of RazorEngine)