aspnetcore: .NET 8 migration from WASM to Blazor Web App - environment not set correctly

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have followed the migration guide from Blazor WebAssembly to Blazor Web App. After migrating, IWebAssemblyHostEnvironment always returned that I’m running the Production environment.

From what I have tested, I can see that < .NET 8 the response to blazor.boot.json always returned a Blazor-Environment header. With the changes described in the migration guide, this is no longer the case and the client cannot determine its environment.

I propose:

  • to include a hint regarding this in the migration guide.
  • to add a middleware that sets this header again.

My team is totally dissatisfied with the options in the Blazor environment docs. Because they have big disadvantages:

  • setting in startup environment doesn’t work as described in the docs with the changed App.razor in the Server project. (It now hase a Routes component on the server and there is no script tag as in the code example.) Additionally, it is just a heuristic based on the URL instead of a correct way of setting the environment.
  • setting the environment header via web.config is not dynamic.

Expected Behavior

IWebAssemblyHostEnvironment should return the correct environment after following the migration guide.

Steps To Reproduce

Create a Blazor WebAssembly app that uses IWebAssemblyHostEnvironment on the client and follow the migration guide to a Blazor Web App.

Exceptions (if any)

No response

.NET Version

8

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

~I’ll get on that update tomorrow (Thursday).~

UPDATE (10/24): Done! Fixed by https://github.com/dotnet/AspNetCore.Docs/pull/33912. I’m only updating the Blazor-to-Blazor migration guidance at 7.0 to 8.0. I’m not changing our integration guidance for adding Blazor to a RP/MVC app because the guidance to place Antiforgery Middleware after Routing Middleware still stands in RP/MVC apps.

In the repro project, removing the call to UseRouting() seems to have fixed the problem.

Maybe we should suggest doing so in the upgrade guidance? I’ve seen other issues in this repo that are caused by explicitly calling UseRouting() (example, possibly related root cause).

@guardrex perfect.

One other minor thing I have just noticed (it’s just about wording). Step 11 of the WASM migration guide mentions that one has to position

app.UseAntiforgery immediately after the call to app.UseRouting

After two sentences it explains to move app.UseAntiforgery after app.UseAuthentication and app.UseAuthorization.

It reads a little strange that it should immediately follow UseRouting, but then later it should basically immediately follow UseAuthorization (if it exists).

It is a bit nit-picky, but I would remove the “immediately” wording. (It caused me to miss the last sentence of that paragraph during one upgrade.)

@guardrex I don’t think you’ve read my comment.

The main branch is the .NET 6 WASM template that is correctly displaying the Hosting environment client-side. In the update-branch I followed your migration guide and the environment is always displayed as “Production”.

The update branch is and has been updated 1:1 according to your migration guide as far as I can tell.