adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
565 stars 84 forks source link

RazorEngineCompilationException for Strongly typed model. #113

Closed Kirmiir closed 1 year ago

Kirmiir commented 2 years ago

Following the example for Strongly typed model usage, I get the following error when I try to complie simple template:

Unable to compile template: (28,69): error CS1031: Type expected
(28,69): error CS1003: Syntax error, ',' expected
(28,70): error CS1003: Syntax error, ',' expected
(28,45): error CS0308: The non-generic type 'RazorEngineTemplateBase' cannot be used with type arguments
(31,67): error CS0115: 'Template.ExecuteAsync()': no suitable method found to override
(33,13): error CS0103: The name 'WriteLiteral' does not exist in the current context
tl1k43uj.51l(5,2): error CS0103: The name 'Write' does not exist in the current context
tl1k43uj.51l(5,8): error CS0103: The name 'Model' does not exist in the current context

After looked on the issue it seems like the problem is related to lack of namespace in my code. After adding namespace into the code all works as expected. The problem here is if there is no namespace the result of the following metod RazorEngineCompilationOptionsBuilder.RenderTypeName is ".ModelName".

The inital thought was to add something looks like:

if (string.IsNullOrEmpty(type.Namespace))
{
    result = type.Name;
}

At the same time there is one case with the similar result. If the Model is inner class the class name will be incorrect.

I haven't found issues or details about it in documentation.

Could someone please advise are these known issues and is there info about in document? Just thought that maybe it worth to add if there is no info.

In both cases there is simple workaround.

Just in case the code example can be found here: Exampe method Example1 for issue with namespace method Example2 for issue with inner class

adoconnection commented 1 year ago

Hi @Kirmiir thanks for detailed info, it helped a lot! version 2022.8.1 is out šŸ‘