9

It seems in a standard Blazor server app, the _content folder items are not being referenced correctly for anything other than the Development environment. As an example, this reference fails in any non-dev environment:

from _Host.cshtml:

<link href="_content/Blazored.Typeahead/blazored-typeahead.css" rel="stylesheet" />

To Repro, using Blazored-toast lib as an example (but any static file refs seem to have this issue):

Create a new Blazor Server project (dotnet new blazorserver)

  1. Add all necessary Blazored / Toast elements, including code to demo a toast message
  2. Test that toast is working Change launchSettings.json ASPNETCORE_ENVIRONMENT to Staging, Production, or anything other than Development
  3. Run program again (using ISS Express local debug), and notice the css formatting is not correct
  4. Change debug settings to use Kestrel instead (change IIS Express drop-down to BlazorApp1 or similar)
  5. Notice with kestrel, css is working fine

What am I missing that would allow this reference to work in other environments?

4
  • Apparently my question has been edited without my permission, to add odd formatting- is this a new thing, anyone can edit your questions now? Commented Nov 18, 2019 at 5:17
  • I thought it was ok to format the question. I'm sorry if you don't like this. "anyone can edit your questions now?" : Yes. Everyone with enough rep can edit other threads. There will be a history if you want to review this.
    – itminus
    Commented Nov 20, 2019 at 0:55
  • Sorry, didn't mean to be critical, I appreciate the intent, it just threw me off. I guess there's no harm in the formatting, it's just my preference, for readability, to keep the paragraph all one font/size, etc. and the code snippets a different font. I see the history but don't see any way to reject or undo the changes. No problem, just seems odd to me that other non-moderator members can override the OP's content. Commented Nov 21, 2019 at 1:17
  • I roll it back to your original content.
    – itminus
    Commented Nov 21, 2019 at 1:20

1 Answer 1

15

Consuming static assets from a Razor Class Library works out of the box when the application gets published. You just have to include the static content via <link href="..." /> as you did.

However, when running the app from the build output (dotnet run) or via F5 in Visual Studio you have to ensure that the StaticWebAsset feature is enabled for the given environment.

It is enabled by default for the development environment only. You can turn on the feature unconditionally by ensuring you called UseStaticFiles and calling UseStaticWebAssets in the Program.CreateHostBuilder.

So, ensure that you consuming app has :

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseStaticFiles();

    ...
}

and in your Program.cs you should have

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStaticWebAssets();
            webBuilder.UseStartup<Startup>();
        });
5
  • Thanks for this answer, I'll try it once I get back in the office on Monday. I had the app.UseStaticFiles() but not the .UserStaticWebAssets(). It's odd to me that nowhere can you tell, from the code, that this is needed for Staging and Production but not for Development environments. None of these code changes reference an environment. Commented Nov 17, 2019 at 6:11
  • 1
    Documentation on consuming content from referenced Razor Class Libraries has few more relevant details Commented Feb 28, 2020 at 13:56
  • 1
    Worked for me +1.. Was getting 404 on _content style and scripts resources, but only with staging and production environments run in VS. Problem resolved after adding UseStaticWebAssets()
    – Yogi
    Commented Oct 12, 2020 at 15:31
  • 1
    How would I set this if I were using WebApplication.CreateBuilder(args)? Commented Apr 25, 2022 at 17:49
  • New question posted in regards to the question in my previous comment: stackoverflow.com/questions/72003834/… Commented Apr 25, 2022 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.