I've had great success so far just using the following code to compile templates. However, one of my templates is compiled into invalid code with a syntax error by Razorengine. Another template works fine. I am using the latest version of razorengine nuget offers me 3.0.0.
var templateFilePath = Path.Combine(_viewPath, templateName + ".cshtml");
string templateText = File.ReadAllText(templateFilePath);
_host.AddAssemblyFromType(model.GetType());
templateId = _host.CompileTemplate(templateText);
if (templateId == null)
throw new Exception(_host.ErrorMessage); // <-- this is where I get the code below
The specific part of the leading part of the auto-generated template code that is invalid (note that it is missing the '>'):
The leading part of the auto-generated template code:
An unhandled exception of type 'System.Exception' occurred in COMPANY.Business.dll
Additional information: Line: 20, Column: 148, Error: Syntax error, '>' expected
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace @__RazorHost {
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Westwind.RazorHosting;
public class _68d0400f_ad4b_488e_9806_f400ac72b094 : Westwind.RazorHosting.RazorTemplateBase<COMPANY.Business.Models.MODELNAME {
#line hidden
My template starts with (note that I've replaced text-specific to my employer with capitals):
I've had great success so far just using the following code to compile templates. However, one of my templates is compiled into invalid code with a syntax error by Razorengine. Another template works fine. I am using the latest version of razorengine nuget offers me 3.0.0.
The specific part of the leading part of the auto-generated template code that is invalid (note that it is missing the '>'):
The leading part of the auto-generated template code:
My template starts with (note that I've replaced text-specific to my employer with capitals):