dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.9k stars 9.86k forks source link

Blazor Server sites break when ASPNETCORE_ENVIRONMENT is set to anything other than Development, Staging, or Production #55426

Open vsfeedback opened 2 months ago

vsfeedback commented 2 months ago

This issue has been moved from a ticket on Developer Community.


I had a Blazor Server site started in .NET6 that was upgraded to 7 then to 8. I changed the ASPNETCORE_ENVIRONMENT to "Local". The site stopped working. It would load the initial HTML, but none of the interactivity was there and none of the CSS styles were there.

The community on reddit (/r/Blazor) helped me understand that I needed the below statement in my program.cs.

StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);

It is unclear to me if this is a bug in Blazor or ASPNET Core, or if it's not a bug and just deep knowledge that a programmer is expected to have. Regardless, Visual Studio could detect and warn about this. If the ASPNETCORE_ENVIRONMENT variable is set to a non-standard value, warn the programmer that they need to configure static web assets.

This issue is easy to reproduce. Start a Blazor Server project from template and change the environment variable and you will see the issue. Add the above statement and you will observe the issue as resolved.


Original Comments

Miller, Michael on 1/8/2024, 06:45 AM:

(private comment, text removed)

Feedback Bot on 1/8/2024, 09:36 PM:

(private comment, text removed)

guardrex commented 2 months ago

I understand that this issue is about a warning of some sort; but just in case we need to make an article update, the coverage on calling UseStaticWebAssets is in the Static Files article ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-8.0#static-files-in-non-development-environments

garrettlondon1 commented 2 months ago

I ended up having my Development environment in Azure as AzureDevelopment appsettings, and I had "local" as Development in order to make this work.