This project is read-only.

Sections are not supported in this code generation environment?

Jun 20, 2011 at 3:07 AM

Hi I have this snippet that wont compile:

const string html = "This is my sample template, @section Title { <h1>Hello @Model.Name!</h1> }";
Razor.Compile(html, "simple");

Which throws the error:

System.InvalidOperationException : Sections are not supported in this code generation environment.
at System.Web.Razor.Generator.RazorCodeGenerator.VisitSpan(SectionHeaderSpan span)
at System.Web.Razor.Generator.RazorCodeGenerator.TryVisit(Span baseSpan, Action`1 subclassVisitor)
at System.Web.Razor.Generator.RazorCodeGenerator.TryVisitSpecialSpanCore(Span span)
at System.Web.Razor.Generator.RazorCodeGenerator.VisitSpan(Span span)
Anyone knows how to fix/support?

Jun 20, 2011 at 3:27 AM

Ok I've tracked it to this piece of code in Razor that's throwing the exception:

protected internal virtual void VisitSpan(SectionHeaderSpan span)
            if (!this.Host.GeneratedClassContext.AllowSections)
                throw new InvalidOperationException(RazorResources.CodeGenerator_SectionsNotSupported);
            this.CurrentBlock.Writer.WriteStartLambdaDelegate(new string[0]);
            this.InSection = true;

Which is because your RazorEngineHost does not specify a value for the 'DefineSectionMethodName' property (
in line 138 in CompilerServiceBase.cs:

            var host = new RazorEngineHost(CodeLanguage, () => MarkupParser)
                               DefaultBaseClass = BuildTypeName(templateType, modelType),
                               DefaultClassName = className,
                               DefaultNamespace = "CompiledRazorTemplates.Dynamic",
                               GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral",
                                                                                 "WriteTo", "WriteLiteralTo",

Is there any reason why this isn't/can't be implemented?

Mar 6, 2012 at 10:47 AM

Have you figured out any solutions for this? I need to use this functionality as well for my implementation.

Mar 6, 2012 at 6:15 PM


What version are you using? If you are still using v2.1, then you may want to start looking at migrating to v3. We're currently pushing v3.0.8beta, but I am hoping to get the final version out the door. The new home for v3 is