toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.5k stars 259 forks source link

Exception : One or more section(s) have been ignored. Ignored section(s) #535

Open sybaris opened 7 months ago

sybaris commented 7 months ago

Describe the bug The following code throw an exception "System.InvalidOperationException: 'One or more section(s) have been ignored. Ignored section(s): 'data''"

To Reproduce

   internal class Program
    {
        const string LAYOUT = @"
    @{ if (Model.Count>0)
        {
            @RenderSection(""data"", required: false)
        }
    }
    @RenderBody()";

        const string PAGE = @"
@{
    Layout = ""_Layout.cshtml"";
}

@section data {
    <p>This will be rendered inside Layout</p>
}";

        static async Task Main(string[] args)
        {
            var engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(Program))
                .AddDynamicTemplates(new Dictionary<string, string>() { { "_Layout.cshtml", LAYOUT } })
                .SetOperatingAssembly(typeof(Program).Assembly)
                .UseMemoryCachingProvider()
                .Build();

            // This runs ok.
            string result1 = await engine.CompileRenderStringAsync("templateKey", PAGE, new List<string>() { "1" });
            Console.WriteLine(result1);
            // This raise an exception "System.InvalidOperationException: 'One or more section(s) have been ignored. Ignored section(s): 'data''"
            string result2 = await engine.CompileRenderStringAsync("templateKey", PAGE, new List<string>() );
            Console.WriteLine(result2);
        }
    }

Expected behavior I expect to not have an exception. Or I except to have a flag somewhere to override this check.

Information (please complete the following information):