Blazor 服务器 .NET6 Razor 类库资产 404 错误

Mar*_*per 7 asp.net-core razor-class-library asp.net-core-6.0

我有一个 Razor 类库,我已经在 .NetCore3 和 .NetCore 5 上使用了几个月,没有出现任何问题。

最近将我们的 blazor 服务器应用程序和 Razor 类库更新到 .NetCore 6 后,我在从 Razor 类库加载资源时遇到了问题。

RCL 是通过 nuget 构建和打包的,我可以看到包中的资产,例如; Nuget 包示例

Web 应用程序已更新为使用单个 program.cs,我正在使用WebApplication.CreateBuilder()我的设置选项。

        var builder = WebApplication.CreateBuilder(new WebApplicationOptions
        {
            ApplicationName = typeof(Program).Assembly.FullName,
            ContentRootPath = Directory.GetCurrentDirectory(),
            EnvironmentName = Environments.Development,
            WebRootPath = ""
        });
Run Code Online (Sandbox Code Playgroud)

加载这些资源时出现 404 错误

    <link href="_content/Blurred.Framework.Web.Shared/favicon.ico" type="image/x-icon" rel="icon" />
    <link href="_content/Blurred.Framework.Web.Shared/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="_content/Blurred.Framework.Web.Shared/css/site.css" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)

我还可以看到wwwroot文件夹和相应的资产已加载到 Visual Studio 中的应用程序项目中。 VS 中的 Web 应用程序

感觉这是一个配置问题,而不是需要更改的重大问题。

ContentRootPath或 的正确设置是什么WebRootPath


更新

据此我需要使用app.UseStaticFiles();我已经完成的内容,以及.NET6中未使用的webBuilder.UseStaticWebAssets();内容:(ConfigureWebHostDefaults

Mar*_*per 1

所以我的问题在于 Azure 构建 RCL 解决方案并将 RCL 打包在 nuget 包中的方式。

我必须更新我的构建 YML 以使用 2022 映像以及 .NET 和 nuget v6.0:

改变了

pool:
  vmImage: 'windows-latest'
Run Code Online (Sandbox Code Playgroud)

pool:
  vmImage: 'windows-2022'
Run Code Online (Sandbox Code Playgroud)

并添加了

    - task: UseDotNet@2
      displayName: 'Use dotnet 6'
      inputs:
        version: '6.0.x'
Run Code Online (Sandbox Code Playgroud)

并改变了

    - task: NuGetToolInstaller@1
Run Code Online (Sandbox Code Playgroud)

    - task: NuGetToolInstaller@1
      inputs:
        version: 6.0.0
Run Code Online (Sandbox Code Playgroud)

并改变了

    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
        vsVersion: '17.0'
Run Code Online (Sandbox Code Playgroud)

    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
Run Code Online (Sandbox Code Playgroud)