Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 577 forks source link

Auto-generated code from CompileTemplate is invalid #437

Closed rmtew closed 7 years ago

rmtew commented 7 years ago

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 '>'):

Westwind.RazorHosting.RazorTemplateBase<COMPANY.Business.Models.MODELNAME {

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):

@model COMPANY.Business.Models.MODELNAME
firedog commented 7 years ago

The namespace Westwind.RazorHosting leads me to believe that this question should be in https://github.com/RickStrahl/Westwind.RazorHosting

rmtew commented 7 years ago

Oops, sorry. Will go there.