1

I have create a Blazor WebAssembly with authentication, defined the database in json setting file and Update-database by nuget console. I ran the application it works fine after for customization purpose i have added the identity scaffolding features by clicking Add => new Scaffolding item on my server project, then I select all the customization item/layout for it and provided the modal for it. After running the app it is still working for the Home page but when I clicked on the Login/Register menu it getting the following error

An unhandled exception occurred while processing the request. InvalidOperationException: The following sections have been defined but have not been rendered by the page at '/Pages/Shared/_Layout.cshtml': 'Scripts'. To ignore an unrendered section call IgnoreSection("sectionName"). Microsoft.AspNetCore.Mvc.Razor.RazorPage.EnsureRenderedBodyOrSections()

System.InvalidOperationException: The following sections have been defined but have not been rendered by the page at '/Pages/Shared/_Layout.cshtml': 'Scripts'. To ignore an unrendered section call IgnoreSection("sectionName"). at Microsoft.AspNetCore.Mvc.Razor.RazorPage.EnsureRenderedBodyOrSections() at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable1 statusCode) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable1 statusCode) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) at IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

3
  • i did it but no luck Commented Apr 19, 2020 at 20:56
  • Did you solve this @AlamdarKhan? I am facing exactly the same issue, but I'm afraid the selected answer doesn't look good as it will break all the scripts won't it? Strangely, I did this same exercise with the pre release code back in Feb (Ish) and it worked fine then. Its obviously a scaffolding issue, but I really don't like it when scaffolding (something thats meant to help) breaks the app.. Commented Jun 13, 2020 at 8:43
  • Yes, use the code below for each page you are getting error <div class="content px-4"> @RenderBody() @{ @if (IsSectionDefined("Scripts")) { IgnoreSection("Scripts"); } } </div> Commented Jun 14, 2020 at 10:27

2 Answers 2

4

Eliminating the generated file Areas/Identity/Pages/Account/Shared/_ViewStart.cshtml solves this issue without changing anything in _Layout.cshtml

You will also see that the scaffolder generates Pages/Shared/_LoginPartial.cshtml in , which I eliminated because it was redundant. From the start there is one in Areas/Identity/Pages/Account/Shared/_LoginPartial.cshtml which is the one I kept.

If you do add the

@RenderSection("Scripts", false)

Part you will notice that the navbar disappears because the project stops recognizing the existence of the _LoginPartial.cshtml even if you do have the original and the generated one.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, bárbaro! gracias Carlos, only thing is in my case that file _ViewStart.cshtml was under /Areas/Identity/Pages/ . Cheers.
3

Than means, than on your _Layout.cshtml page there is a statement

@RenderSection("Scripts")

And there is no

@section Scripts {
...
}

On the page, using _Layout.cshtml as a layout page. To prevent such errors, you can change

@RenderSection("Scripts")

into

@RenderSection("Scripts", false)

which will make the Scripts section not mandatory for pages, using this layout.

4 Comments

this is default code in _Layout.cshtml @{ Layout = "/Areas/Identity/Pages/_Layout.cshtml"; } html here @section Scripts { @RenderSection("Scripts", required: false)
Ah, that's exactly the opposite situation. Your child component defines the section Scripts, which is not defined in the parent view. @section Scripts { @RenderSection("Scripts", required: false) implies that a _Layout has a parent view itself and there a Scripts section should be defined. Either put @RenderSection("Scripts") into parent View, or put there @IgnoreSection("Scripts") (I'm not sure that's good idea, because your scripts can vanish from resulting html).
Or just change @section Scripts { @RenderSection("Scripts", required: false) } into @RenderSection("Scripts", required: false). I'm almost sure, that you do not use an outer layout over the _Layout.cshtml and that weird code is just a glitch of template creator.
This is valid, but in my case it also caused the navbar to disappear which is a strong visual contrast. In my answer I covered a way to make it work as expected without this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.