Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Engine not working in ASP.net 5 MVC 6

I am trying to migrate some existing code from MVC5 to MVC6 and I am having difficulty with this particular code:

 Engine.Razor.RunCompile(File.ReadAllText(emailTemplatePath), "emailTemplateKey", typeof (EmailViewModel), emailViewModel);

I am receiving the following runtime error:

MissingMethodException: Method not found: "Void Microsoft.AspNet.Razor.CodeGenerators.GeneratedClassContext.set_ResolveUrlMethodName(System.String)". in RazorEngine.Compilation.CompilerServiceBase.CreateHost(Type templateType, Type modelType, String className)

The original code I was using in MVC5 was taken from here. If there is no way of converting the above code to work with MVC6 what is another elegant way of doing email templates?

like image 217
Cool Breeze Avatar asked Jan 27 '16 07:01

Cool Breeze


1 Answers

Apparently there has been a change in GeneratedClassContext class - the property ResolveUrlMethodName does not exist anymore, hence the MissingMethodException. Looks like ParserContext class has changed too, since accessing OnError event handler throws the same exception.

In fact it is the setter of the missing property missing (pardon the expression!), which, being a method, causes the exception. Absolutely accurate but somewhat misleading, unless you recall this.

Quite a similar question (and a good answer with alternative solution!) here: RazorEngine and MVC 6 beta 7.

like image 153
Alexander Christov Avatar answered Oct 23 '22 14:10

Alexander Christov