An exception of the "RazorEngine. Templating. TemplateCompilationException" type occurs in RazorEngine. NET4.0.dll, but not in user code. razortemplate
Error message:
An exception of the "RazorEngine. Templating. TemplateCompilationException" type occurs in RazorEngine. NET4.0.dll, but not in user code.
Other information: Unable to compile template. "object" does not contain the definition of "username, and the extension method "username" for the first parameter of the acceptable type "object" cannot be found (is the using instruction or assembly reference missing ?)
Other compilation errors may have occurred. Check the Errors property for more information.
Solution:
We need to serialize the model passed to cshtml and pass it in the past. Otherwise, an error will be reported.
1. It may be that the model passed in is an anonymous class, but sometimes this error is reported if a class is declared. (You can try to declare a class to pass in the past)
2. If the first method does not work, you can only use the following method to serialize the model and pass it to cshtml. This method returns a dynamic model. As follows:
static dynamic ToDynamic(object obj){ string json = JsonConvert.SerializeObject(obj); dynamic dyObj = JsonConvert.DeserializeObject(json); return dyObj;}
This method requires introducing the Newtonsoft. Json. dll library (using the latest. net4.5 library)
PS: it is best to put it at the beginning of cshtml: (case sensitive)
@model dynamic
This time, the above error will not be reported when compiling the cshtml code.