really battling to compile a template with a custom model.

Jan 6, 2014 at 9:49 AM
Edited Jan 6, 2014 at 11:54 AM
Hi,

This code below returns several errors in the TemplateCompilationException.
line: 17 33 CS1031 Type expected
line: 17 33 CS1519 Invalid token '.' in class, struct, or interface member declaration

I am running the code in linqpad.
...

There is no line 17 in my template?
void Main()
{
    var config = new TemplateServiceConfiguration();
    config.BaseTemplateType = typeof(TemplateSubject<>);
    config.Language = RazorEngine.Language.CSharp;
    config.EncodedStringFactory = new RawStringFactory();
    var service = new RazorEngine.Templating.TemplateService(config);
    string txt = @"
                @model MyModel
                @{Subject = ""Test Model Base Prop"";}
                 <html>
                 <head><title>@Subject</title></head>
                 </html>
                 ";
                 RazorEngine.Razor.SetTemplateService(service);  
    try {
        var s = RazorEngine.Razor.Parse(txt, new MyModel(), null, null);
    } catch (TemplateCompilationException ex) {
        ex.Errors.Dump();
    }
}

public class MyModel {
    public string Test {
        get;
        set;
    }
}

public abstract class TemplateSubject<T> : RazorEngine.Templating.TemplateBase<T> {
    public string Subject {
        get;
        set;
    }
}
Jan 7, 2014 at 8:19 AM
I solved this by discovering that
class TemplateSubject<T>
Must be located in a namespace. Linqpad does not use a namespace by default, the namespace is blank. Solved by putting the class in an external referenced assembly using a namespace.
Dec 24, 2015 at 2:52 AM
Edited Dec 24, 2015 at 2:52 AM
When i implement same the above i am Getting Error, please help

Errors while compiling a Template.

Please try the following to solve the situation:
  • If the problem is about missing/invalid references or multiple defines either try to load

    the missing references manually (in the compiling appdomain!) or

    Specify your references manually by providing your own IReferenceResolver implementation.

    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.

    Currently all references have to be available as files!
  • If you get 'class' does not contain a definition for 'member':
    try another modelType (for example 'null' to make the model dynamic).
    
    NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
  • error: (17, 97) The type or namespace name 'MyModel' could not be found (are you missing a using directive or an assembly reference?)
Temporary files of the compilation can be found in (please delete the folder): C:\Users\ADMIN\AppData\Local\Temp\RazorEngine_x4gmahv5.vtm

The template we tried to compile is:

------------- START -----------
            @model MyModel

            @{Subject = "Test Model Base Prop";}

             <html>

             <head><title>@Subject</title></head>

             </html>


------------- END -----------



The generated source code is:

------------- START -----------

//------------------------------------------------------------------------------

// <auto-generated>

// This code was generated by a tool.

// Runtime Version:4.0.30319.17929

//

// Changes to this file may cause incorrect behavior and will be lost if

// the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------



namespace CompiledRazorTemplates.Dynamic {
using System;

using System.Collections.Generic;

using System.Linq;





public class RazorEngine_03d159179bc446f3a622bfd148466837 : MvcApplication4.TemplateSubject<MyModel> {







    public RazorEngine_03d159179bc446f3a622bfd148466837() {

    }



    public override void Execute() {
WriteLiteral("\r\n");





WriteLiteral(" ");
              Subject = "Test Model Base Prop";
WriteLiteral("\r\n <html>\r\n <head><title>");
                     Write(Subject);


WriteLiteral("</title></head>\r\n </html>\r\n ");
    }

}
}



------------- END -----------





List of loaded Assemblies:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Caching\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.Caching.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Build.Utilities.v4.0\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.Build.Utilities.v4.0.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.JScript\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.JScript.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Deployment\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Deployment.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WebMatrix.WebData\v4.0_2.0.0.0__31bf3856ad364e35\WebMatrix.WebData.dll

Loaded Assembly: C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.Web.PageInspector.Loader\v4.0_1.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Web.PageInspector.Loader.dll

Loaded Assembly: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\31b0488c\dc559b41\assembly\dl3\dceece5b\a0798726_f33dd101\System.Web.Optimization.dll

Loaded Assembly: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\31b0488c\dc559b41\assembly\dl3\16e2f6cb\463ed226_f33dd101\Microsoft.Web.WebPages.OAuth.dll

Loaded Assembly: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\31b0488c\dc559b41\assembly\dl3\2b77422f\f00cdb25_f33dd101\Microsoft.Web.Mvc.FixedDisplayModes.dll

Loaded Assembly: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\31b0488c\dc559b41\assembly\dl3\c2c3684a\9a704a26_f33dd101\System.Web.Http.WebHost.dll