From 25d1449d29cfb1e41ac1bd4fb9018cde6dc17b09 Mon Sep 17 00:00:00 2001 From: haileymck Date: Tue, 19 May 2026 11:48:29 -0700 Subject: [PATCH] Fix ManageLayout.razor uses wrong layout namespace for WASM/Auto Global Blazor projects In Blazor WASM/Auto (Global interactivity) projects, MainLayout.razor lives in the client project (e.g. BlazorApp1.Client), not the server project's Components/Layout/ folder. The scaffolder was always generating: @layout BlazorApp1.Components.Layout.MainLayout which causes CS0234 because the Layout namespace doesn't exist under BlazorApp1.Components in WASM/Auto Global projects. Fix: detect whether Components/Layout/MainLayout.razor exists in the server project directory. If it does (Blazor Server), use the .Components.Layout namespace. If not (WASM/Auto Global), use .Client.Layout instead. Fixes both the dotnet-scaffold (ValidateIdentityStep) and the VS scaffolder (BlazorIdentityGenerator) code paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../BlazorIdentity/BlazorIdentityGenerator.cs | 7 ++++++- .../AspNet/ScaffoldSteps/ValidateIdentityStep.cs | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Scaffolding/VS.Web.CG.Mvc/BlazorIdentity/BlazorIdentityGenerator.cs b/src/Scaffolding/VS.Web.CG.Mvc/BlazorIdentity/BlazorIdentityGenerator.cs index 987be5ff2c..c399bb7903 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/BlazorIdentity/BlazorIdentityGenerator.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/BlazorIdentity/BlazorIdentityGenerator.cs @@ -171,7 +171,12 @@ internal async Task ValidateAndBuild(BlazorIdentityCommandL } var rootIdentityNamespace = $"{commandlineModel.RootNamespace}.Components.Account"; - var layoutNamespace = $"{commandlineModel.RootNamespace}.Components.Layout.MainLayout"; + // For WASM/Auto Global Blazor projects, MainLayout lives in the client project (e.g. BlazorApp1.Client). + // For Blazor Server projects, it lives directly under Components/Layout/ in the server project. + var mainLayoutInServerProject = Path.Combine(AppInfo.ApplicationBasePath, "Components", "Layout", "MainLayout.razor"); + var layoutNamespace = FileSystem.FileExists(mainLayoutInServerProject) + ? $"{commandlineModel.RootNamespace}.Components.Layout.MainLayout" + : $"{commandlineModel.RootNamespace}.Client.Layout.MainLayout"; var defaultDbContextNamespace = $"{commandlineModel.RootNamespace}.Data"; var defaultUserNamespace = $"{commandlineModel.RootNamespace}.Data"; var blazorIdentityModel = new BlazorIdentityModel diff --git a/src/dotnet-scaffolding/dotnet-scaffold/AspNet/ScaffoldSteps/ValidateIdentityStep.cs b/src/dotnet-scaffolding/dotnet-scaffold/AspNet/ScaffoldSteps/ValidateIdentityStep.cs index fe5d105c11..ada9d2638a 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold/AspNet/ScaffoldSteps/ValidateIdentityStep.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold/AspNet/ScaffoldSteps/ValidateIdentityStep.cs @@ -213,7 +213,16 @@ public override async Task ExecuteAsync(ScaffolderContext context, Cancell if (!string.IsNullOrEmpty(projectName)) { identityNamespace = settings.BlazorScenario ? $"{projectName}.Components.Account" : $"{projectName}.Areas.Identity"; - identityLayoutNamespace = settings.BlazorScenario ? $"{projectName}.Components.Layout.MainLayout" : string.Empty; + if (settings.BlazorScenario) + { + // For WASM/Auto Global Blazor projects, MainLayout lives in the client project (e.g. BlazorApp1.Client). + // For Blazor Server projects, it lives directly under Components/Layout/ in the server project. + var mainLayoutInServerProject = Path.Combine(projectDirectory, "Components", "Layout", "MainLayout.razor"); + identityLayoutNamespace = _fileSystem.FileExists(mainLayoutInServerProject) + ? $"{projectName}.Components.Layout.MainLayout" + : $"{projectName}.Client.Layout.MainLayout"; + } + userClassNamespace = $"{projectName}.Data"; }