asp.net mvc 4 - RazorEngine Error trying to send email -


i have mvc 4 application sends out multiple emails. example, have email template submitting order, template cancelling order, etc...

i have email service multiple methods. controller calls send method looks this:

public virtual void send(list<string> recipients, string subject, string template, object data) {     ...     string html = getcontent(template, data);     ... } 

the send method calls getcontent, method causing 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 receiving error:

the same key used template!

in getcontent method should add new parameter templatekey , use variable instead of using htmltemplate? new order email template have neworderkey , cancelorderkey email template being used cancel order?

explanation

this happens because use same template key ("htmltemplate") multiple different templates. note way have implemented getcontent run multiple problems:

  • even if use unique key, example template variable, trigger exception when templates edited on disk.

  • performance: reading template file every time when template cached.

solution:

implement itemplatemanager interface manage 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 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:

// don't need method anymore. private string getcontent(string template, object data) {     return engine.razor.runcompile(template, null, data); } 

razorengine fix problems mentioned above internally. notice how fine use name of template key, if in scenario name need identify template (otherwise cannot use nameonlytemplatekey , need provide own implementation).

hope helps. (disclaimer: contributor of razorengine)


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -