toddams/RazorLight

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

Opened this issue · 0 comments

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

  • RazorLight version 2.3.1
  • Visual Studio 2022
  • Dotnet 6.0
    Additional context
    Add any other context about the problem here.