Blazor server .NET6 Razor Class Library assets 404 error

Viewed 766

I have a Razor Class Library that I've been using for months with .NetCore3 then .NetCore 5 without a problem.

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.

The RCL is built and packaged via nuget and I can see the assets in the package, for example; Nuget package example

The web application has been updated to use a single program.cs and I'm using WebApplication.CreateBuilder() with options for my setup.

        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

    <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. web application in VS

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?


Update

According to this I need to use app.UseStaticFiles(); which I have done, and also webBuilder.UseStaticWebAssets(); from within ConfigureWebHostDefaults which isn't used in .NET6 :(

3 Answers

So my problem was with the way Azure was building the RCL solution and packaging the RCL in a nuget package.

I had to update my build YML to use 2022 image, and v6.0 of .NET and nuget:

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. For this reason, the resource link did not work anymore. It refers to the assembly name, not the Nuget package name.

After I changed it so the 2 names were the same, It worked.

I had this with a project originally created as netcoreapp3.1. The fix was calling the extension methods

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

Both are in the BlazorTable namespace (using BlazorTable;)

Related