升级至.NET 8.0.200后Blazor WebAssembly无法定位App.razor
Blazor WebAssembly项目升级后编译错误及空白页问题
将Visual Studio升级至17.9.0版本、项目升级至.NET 8.0.200后,Blazor WebAssembly项目无法运行。在Program.cs的代码builder.RootComponents.Add<App>("#app");处,出现错误:
error CS0246: The type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)
尽管App.razor与Program.cs处于同一命名空间中。尝试添加app.razor.cs后项目可编译,但加载完成后显示空白页面,且无任何错误提示。
App.razor 代码
@using MyApp.Client; <CascadingAuthenticationState> <Router AppAssembly="@typeof(App).Assembly"> <Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> <FocusOnNavigate RouteData="@routeData" Selector="h1" /> </Found> <NotFound> <PageTitle>Not found</PageTitle> <LayoutView Layout="@typeof(MainLayout)"> <p role="alert">Sorry, there's nothing at this address.</p> </LayoutView> </NotFound> </Router> </CascadingAuthenticationState>
Program.cs 代码
using Blazored.Toast; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Polly; using MyApp.Client.Helper; using Polly.Contrib.WaitAndRetry; using MyApp.Shared.Models; using Microsoft.JSInterop; using System.Globalization; namespace MyApp.Client { public class Program { public static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); builder.Services.AddBlazoredToast(); builder.Services.AddSingleton<EventService>(); builder.Services.AddScoped<AuthenticationDataMemoryStorage>(); builder.Services.AddScoped<BlazorSchoolAuthenticationStateProvider>(); builder.Services.AddScoped<ClipboardService>(); builder.Services.AddScoped<AuthenticationStateProvider>(sp => sp.GetRequiredService<BlazorSchoolAuthenticationStateProvider>()); builder.Services.AddHttpClient("HttpClient", httpClient => { httpClient.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress); }) .AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5))); builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IHttpService, HttpService>(); builder.Services.AddScoped<ILocalStorageService, LocalStorageService>(); builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings")); builder.Services.AddBlazorDialog(); builder.Services.AddAuthorizationCore(); builder.Services.AddLocalization(); var host = builder.Build(); CultureInfo culture; var js = host.Services.GetRequiredService<IJSRuntime>(); var result = await js.InvokeAsync<string>("blazorCulture.get"); if (result != null) { culture = new CultureInfo(result); } else { culture = new CultureInfo("en-US"); await js.InvokeVoidAsync("blazorCulture.set", "en-US"); } CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; await host.RunAsync(); } } }
解决方法
1. 修正App.razor的命名空间声明
在App.razor顶部添加@namespace MyApp.Client(可保留原@using MyApp.Client)。@namespace是声明组件自身所属的命名空间,仅用@using无法让编译器识别组件属于当前命名空间:
@namespace MyApp.Client <CascadingAuthenticationState> <!-- 原有组件内容 --> </CascadingAuthenticationState>
2. 清理编译缓存并重建项目
- 手动删除项目目录下的
bin和obj文件夹 - 执行
dotnet clean命令清理项目 - 重新执行
dotnet build或在Visual Studio中点击"生成解决方案"
3. 检查app.razor.cs的代码隐藏类(若保留)
如果添加了app.razor.cs,确保它是正确的部分类实现,且命名空间与Program.cs一致:
namespace MyApp.Client; public partial class App : ComponentBase { // 仅添加必要的组件逻辑,避免冲突 }
4. 排查空白页问题
- 打开浏览器开发者工具(F12),切换到"控制台"标签页查看是否有JavaScript错误
- 在Program.cs中添加日志输出,帮助定位渲染问题:
var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.Logging.SetMinimumLevel(LogLevel.Debug); // 添加这行
5. 验证项目文件配置
检查项目的.csproj文件,确保TargetFramework和Blazor SDK配置正确:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <!-- 其他依赖项配置 --> </Project>
内容的提问来源于stack exchange,提问作者Taufiq Abdur Rahman
相关产品推荐
相关产品推荐

