7

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 :(

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
  • 1
    According to your description this is an issue which comes from migration, and can we understand it as an issue about how to integrate your static css file stored in wwwroot directory in .net 6? – Tiny Wang Nov 26 '21 at 05:43
  • @TinyWang Yes, an RCL that _was_ working in .NET 5 is not working in .NET 6 – Mark Cooper Nov 26 '21 at 07:37

3 Answers3

1

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)'
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
0

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.

Rye bread
  • 1,305
  • 2
  • 13
  • 36
0

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;)

gregmac
  • 24,276
  • 10
  • 87
  • 118