Search code examples
asp.net-core.net-corerazorconfiguration

dotnet core 2.1 error in rendering view after adding html code


I have a razorview which uses a layout with name ReportsLayout. I was pulling configuration strings by injecting IConfiguration into the Layout as well as page views as below.

_Layout View @using Microsoft.Extensions.Configuration @inject IConfiguration Configuration

Page View @using Microsoft.Extensions.Configuration @inject IConfiguration Configuration @model DayFuelSalesView @{ Layout = "~/Views/Shared/_ReportsLayout.cshtml"; }

It was working fine until I added some more code to the razorview page which is basically pure html code.

After adding the code, during the debugging, the debugger show error as below in the web browser while trying to open the page. Error Details

This is the first time I am seeing such kind of error and I am unable to get it to work. Any help is appreciated.


Solution

  • Try to check if you have @Render xxx{} in your Page View.If so,you need to add @await RenderSectionAsync("xxx", required: false) to your _Layout View.Try to refer to the Sections in the official doc.For example,if you have the following code in your view:

    @section Scripts
    {
        ...
    }
    

    You need to make sure you have @await RenderSectionAsync("Scripts", required: false) in your _ReportsLayout.cshtml.