question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor web assembly] On page reload for a deployed app, set environment (environment's appsettings file) not being picked

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Following the setup of multiple environments via startup configuration as specified here in this documentation - https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments?view=aspnetcore-7.0#set-the-environment-via-startup-configuration Below is the configuration set in the index.html file

<script>
        document.addEventListener("DOMContentLoaded", function () {
            if (window.location.hostname.includes("localhost")) {
                Blazor.start(); //ensures that @localhost, the default debugging flow can be used.
            } else if (window.location.hostname.includes("test")) {
                Blazor.start({
                    environment: "Test"
                });
            } else if (window.location.hostname.includes("demo")) {
                Blazor.start({
                    environment: "Demo"
                });
            } else {
                Blazor.start({
                    environment: "Production"
                });
            }
        });
    </script>

A standalone wasm app deployed to use an environment like demo works well upon visiting the root domain eg https://example-demo.com. If one navigates to a page like https://example-demo.com/votes, votes page will load as expected. Issue arises when you reload the page. Blazor seems to default to using appsetting.Production.json instead of reloading using appsettings.Demo.json Strangely this works well for test. NB: Deployment is being done by deploying the published assets to object storage or a similar approach to GitHub pages

Expected Behavior

If on the votes page - https://example-demo.com/votes and you reload, blazor is supposed to use the appsettings.Demo.json

Steps To Reproduce

Have in place appsettings for development, test, demo, production. Add a the code snippet above mentioned in the describe bug section to your index.html file. while executing dotnet publish command, specify environment name accordingly using /p:EnvironmentName

Exceptions (if any)

No response

.NET Version

.NET 7

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Reactions:1
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kyandakscommented, Apr 26, 2023

@javiercn @mkArtakMSFT blazor.boot.json throws no error logs. The only error logs are CORS errors since wrong Base URLs are loaded and these API URLs are defined in appsettings for different environments. Below is a screenshot of the network tab in developer tools: blazor-error

NB: BTW Our experiments with docker as a deployment option have this working well.

0reactions
mkArtakMSFTcommented, May 4, 2023

Sounds good. Closing as there is not enough information for us to investigate this further. Glad that you have found a solution anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to replace appsettings.json values for Blazor WASM ...
I ran into this exact same problem with Static Web Apps. The issue is that when published, compressed versions of the appsettings file...
Read more >
Blazor WASM not loading appsettings.{environment}.json in ...
In this blog post, we are going to outline steps that allow developers deploying Blazor WebAssembly projects to workaround a shortcoming related to...
Read more >
Why isn't my ASP.NET Core environment-specific ...
In this post I describe an issue I was having in which my application wouldn't load my appsettings.Development.json file when running in ...
Read more >
Host and deploy ASP.NET Core Blazor WebAssembly
Learn how to host and deploy Blazor WebAssembly using ASP.NET Core, Content Delivery Networks (CDN), file servers, and GitHub Pages.
Read more >
ASP.NET Core Blazor environments
Learn about environments in Blazor, including how to set the environment.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found