0
votes

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)

2
i did it but no luck - Alamdar Khan
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.. - Brett JB
Yes, use the code below for each page you are getting error <div class="content px-4"> @RenderBody() @{ @if (IsSectionDefined("Scripts")) { IgnoreSection("Scripts"); } } </div> - Alamdar Khan

2 Answers

3
votes

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.

3
votes

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.