How do I fix static assets not showing on implementing project with .Net9

William Mendoza 0 Reputation points
2024-12-24T20:51:38.42+00:00

Hi, I am having an issue in my app when trying to implement a Nuget package.

My project has the following configuration:
<ItemGroup>

<!-- Include the JavaScript file in the NuGet package -->

<Content Include="wwwroot\**\*">

<Pack>true</Pack>

<CopyToOutputDirectory>Always</CopyToOutputDirectory>

</Content>

<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />

<PackageReference Include="Microsoft.JSInterop" Version="9.0.0" />

<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

</ItemGroup>

and it shows properly in my project:
User's image

But when I access the file I get a blank page:
User's image

if I try the compressed version I can see the code:
User's image

The _content url does not work:
User's image

//the nuspec file shows:
<contentFiles>

<files include="any/net9.0/wwwroot/scripts/invokegooglecaptcha.js" buildAction="Content" />

</contentFiles>

I reference the file like this:
<script src="@Assets["/scripts/invokegooglecaptcha.js"]" type="text/javascript"></script>

I searched through the documentation but I could not resolve it. Any help is appreciated it. Thank you.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,635 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ruikai Feng - MSFT 2,586 Reputation points Microsoft Vendor
    2024-12-25T06:58:28.9+00:00

    Hi,@William Mendoza,I tried with a minimal example:

    Project structure:

    User's image

    Blazor App project file:

    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
        <TargetFramework>net9.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="TestRCL" Version="1.0.0" />
      </ItemGroup>
    </Project>
    
    
    

    RCL project file:

    <Project Sdk="Microsoft.NET.Sdk.Razor">
    	<PropertyGroup>
    		<TargetFramework>net9.0</TargetFramework>
    		<Nullable>enable</Nullable>
    		<ImplicitUsings>enable</ImplicitUsings>
    		<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    		<PackageId>$(AssemblyName)-x1</PackageId>
    	</PropertyGroup>
    	
    	
    	<ItemGroup>
    		<SupportedPlatform Include="browser" />
    	</ItemGroup>
    	<ItemGroup>
    		<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0" />
    	</ItemGroup>
    </Project>
    

    Program.c of Blazor App:

    var builder = WebApplication.CreateBuilder(args);
    // 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", createScopeForErrors: true);
        
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseAntiforgery();
    app.MapStaticAssets();
    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode();
    app.Run();
    

    It works well on myside:

    User's image

    User's image

    If you still have problems with this issue,please share a minimal example that could reproduce the issue

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.