You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

发布作为类库的Blazor Web App时出现404站点未找到问题

问题:Blazor Web Server类库被控制台应用引用后,AOT发布启动出现404及交互组件错误

现有基础应用引用多个其他应用(包含一个Blazor Web应用),发布该应用并启动EXE后,访问页面出现404错误。Visual Studio的Debug/Release模式下运行正常,但发布后启动应用时,尽管控制台输出符合预期,且发布文件夹中包含wwwroot,仍出现异常。

复现步骤

  • 创建采用.NET 8、AOT发布的控制台应用
  • 创建Blazor Web Server应用
  • 将Blazor应用转为类库
  • 在控制台应用中引用该Blazor类库
  • 编辑初始化类,使控制台应用在独立任务中运行Blazor应用:
using System.Net;
using System.Reflection;
using MyBlazorApp.Components;

namespace MyBlazorApp
{
    public class MyBlazorAppCore
    {
        public void Initialize()
        {
            Task.Run(
                async () =>
                {
                    var builder = WebApplication.CreateBuilder();
                    var env = builder.Environment;
                    var root = "";
                    var webRoot = "";

                    if (env.IsDevelopment())
                    {
                        root = "C:\\MYPATHTOAPP\\";
                        webRoot = "C:\\MYPATHTOAPP\\wwwroot";
                    }
                    else
                    {
                        root = "\\";
                        webRoot = Path.Combine(Assembly.GetExecutingAssembly().Location, "wwwroot");
                    }

                    var options = new WebApplicationOptions()
                    {
                        ContentRootPath = root,
                        ApplicationName = Assembly.GetExecutingAssembly().GetName().Name,
                        WebRootPath = webRoot,
                    };

                    Console.WriteLine(builder.Environment.ContentRootPath);
                    Console.WriteLine(builder.Environment.WebRootPath);
                    Console.WriteLine(builder.Environment.ApplicationName);

                    builder.WebHost.ConfigureKestrel((context, serverOptions) =>
                    {
                        serverOptions.Listen(IPAddress.Loopback, 5000);
                    });

                    // Add services to the container.
                    builder.Services.AddRazorComponents()
                           .AddInteractiveServerComponents();

                    var app = builder.Build();

                    // Configure the HTTP request pipeline.
                    if (!app.Environment.IsDevelopment())
                    {
                        app.UseExceptionHandler("/Error");
                        //app.UseHsts();
                    }

                    //app.UseHttpsRedirection();

                    app.UseStaticFiles();
                    app.UseAntiforgery();

                    app.MapRazorComponents<App>()
                       .AddInteractiveServerRenderMode();

                    await app.RunAsync();
                });
        }
    }
}
  • 配置项目文件,将Blazor项目wwwroot文件夹内容(含scopedcss/bundle/MyBlazorApp.styles.css)复制到输出目录:
<Target Name="CopyBundledCss" AfterTargets="Build;Publish">
  <ItemGroup>
    <BundledCssFiles Include="$(IntermediateOutputPath)scopedcss\bundle\**\*.css" />
  </ItemGroup>
  <Copy SourceFiles="@(BundledCssFiles)" DestinationFolder="$(OutDir)wwwroot" OverwriteReadOnlyFiles="true"/>
</Target>

<Target Name="CopyCssBundles" AfterTargets="AfterBuild">
  <ItemGroup>
    <MyCssBundles Include="$(IntermediateOutputPath)\scopedcss\bundle\$(AssemblyName).styles.css" />
  </ItemGroup>
  <Copy SourceFiles="@(MyCssBundles)" DestinationFiles="wwwroot\%(RecursiveDir)%(Filename)%(Extension)" OverwriteReadOnlyFiles="true" />
</Target>

<ItemGroup>
  <Content Update="wwwroot\**\*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

后续发现的其他异常

  • 默认Counter组件因渲染模式问题出现500错误
  • /Error页面无法正常工作,相关日志如下:
30.05.2024 07:30:40 |   Debug    | 1 candidate(s) found for the request path '/Error'
30.05.2024 07:30:40 |   Debug    | Request matched endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Trace    | The endpoint does not specify the IRequestSizeLimitMetadata.
30.05.2024 07:30:40 |   Debug    | Static files was skipped as the request already matched an endpoint.
30.05.2024 07:30:40 |   Info     | Executing endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Debug    | Error handling in progress. Interactive components are not enabled.
30.05.2024 07:30:40 |   Debug    | Begin render root component 'App' with page 'Error'.
30.05.2024 07:30:40 |   Info     | Executed endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Error    | An exception was thrown attempting to execute the error handler.
30.05.2024 07:30:40 |   Error    | Connection id "0HN40F9JROBR7", Request id "0HN40F9JROBR7:00000002": An unhandled exception was thrown by the application.

恳请提供解决该问题的思路。

内容的提问来源于stack exchange,提问作者3HMonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 08:05:24