简体   繁体   English

Blazor 服务器 .NET6 Razor Class 图书馆资产 404 错误

[英]Blazor server .NET6 Razor Class Library assets 404 error

I have a Razor Class Library that I've been using for months with.NetCore3 then.NetCore 5 without a problem.我有一个 Razor Class 库,我已经使用了几个月 with.NetCore3 then.NetCore 5 没有问题。

After recently updating our blazor server application and our Razor Class Library to.NetCore 6 I've hit a problem loading the assets from the Razor Class Library.在最近将我们的 blazor 服务器应用程序和 Razor Class 库更新为 .NetCore 6 之后,我在从 Razor Class 库加载资产时遇到了问题。

The RCL is built and packaged via nuget and I can see the assets in the package, for example; RCL 是通过 nuget 构建和打包的,我可以看到 package 中的资产,例如; Nuget 包示例

The web application has been updated to use a single program.cs and I'm using WebApplication.CreateBuilder() with options for my setup. web 应用程序已更新为使用单个 program.cs,我正在使用WebApplication.CreateBuilder()和我的设置选项。

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

When loading these resources I'm getting a 404 error加载这些资源时出现 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" />

I can also see the wwwroot folder and respective assets getting loaded into the application project in visual studio.我还可以看到wwwroot文件夹和相应的资产被加载到 visual studio 中的应用程序项目中。 VS 中的 Web 应用程序

It feels like this is a configuration issue, rather than something significant that needs to change.感觉这是一个配置问题,而不是需要更改的重要问题。

What are the correct settings for the ContentRootPath or WebRootPath ? ContentRootPathWebRootPath的正确设置是什么?


Update更新

According to this I need to use app.UseStaticFiles(); 据此我需要使用app.UseStaticFiles(); which I have done, and also webBuilder.UseStaticWebAssets();我已经完成了,还有webBuilder.UseStaticWebAssets(); from within ConfigureWebHostDefaults which isn't used in .NET6:(来自.NET6 中未使用的ConfigureWebHostDefaults :(

So my problem was with the way Azure was building the RCL solution and packaging the RCL in a nuget package.所以我的问题在于 Azure 构建 RCL 解决方案并将 RCL 打包在 nuget package 中的方式。

I had to update my build YML to use 2022 image, and v6.0 of .NET and nuget:我必须更新我的构建 YML 以使用 2022 映像,以及 .NET 和 nuget 的 v6.0:

Changed改变了

pool:
  vmImage: 'windows-latest'

to

pool:
  vmImage: 'windows-2022'

and added并添加

    - task: UseDotNet@2
      displayName: 'Use dotnet 6'
      inputs:
        version: '6.0.x'

and changed并改变了

    - task: NuGetToolInstaller@1

to

    - task: NuGetToolInstaller@1
      inputs:
        version: 6.0.0

and changed并改变了

    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
        vsVersion: '17.0'

to

    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'

I had the same error.我有同样的错误。

In my case I had upgraded the Nuget package to .NET 6 but at the same time changed the assembly (*.dll) so it was different from the package name. In my case I had upgraded the Nuget package to .NET 6 but at the same time changed the assembly (*.dll) so it was different from the package name. For this reason, the resource link did not work anymore.因此,资源链接不再起作用。 It refers to the assembly name, not the Nuget package name.它指的是程序集名称,而不是 Nuget package 名称。

After I changed it so the 2 names were the same, It worked.在我将其更改为两个名称相同之后,它起作用了。

I had this with a project originally created as netcoreapp3.1 .我有一个最初创建为netcoreapp3.1的项目。 The fix was calling the extension methods修复是调用扩展方法

  • IWebHostBuilder.UseStaticWebAssets()
  • IServiceCollection.AddBlazorTable()

Both are in the BlazorTable namespace ( using BlazorTable; )两者都在BlazorTable命名空间中( using BlazorTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM