appsettings.json ignored in .NET Web App

Alex D 16 Reputation points
2024-03-15T14:14:35.7866667+00:00

Hi.

I've recently upgraded from .NET 6 -> 8 and accordingly created a new Web App. I had no code to change relating the configuration files (appsettings.json).

To my surprise, pretty much every value in the JSON file is inaccessible to the code. The only way to pass a configuration value is through Environment Variables in the Azure app menu.

Mind you, this only happens in Azure. Everything works perfectly in localhost, with the same configuration.

Can anyone help me figure out what's wrong?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,831 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 47,716 Reputation points
    2024-03-15T14:49:50.3333333+00:00

    First thing that comes to mind is to confirm that the appsettings.json file is actually copied to the site in Azure. Use the file explorer in Azure to confirm the file got there.

    Second thing is that the Settings portion of Azure takes precedence over any appsettings so check there next. Note that if you have DB connection strings in your settings then it should be pretty easy to confirm whether the Azure site is overwriting the existing settings or not seeing the file at all. Another thing to pay attention to is if you're using slots then check the slot setting of the root app.

    Third thing is if you're using environment settings. The ASPNET environment is configured via Azure settings. So if your Azure site is configured to have an environment of, say, stage then it'll load the appsettings.json and then the appsettings.stage.json over that. Ensure this either doesn't apply to you or that everything is configured correctly.

    Fourth thing is that your app isn't using the standard app builder and is therefore not including the default settings file. If it works locally then I'd confirm that you didn't accidentally make this only applicable to local dev (environment: development).

    Finally, confirm that your site is configure in Azure to properly use a NET 8 app. If it was previously running as NET 6 then the core site should be configured properly unless your deployment rebuilds everything in Azure as well. Under the General Settings in Azure ensure you have configured the stack for NET and the version for NET 8. Also check your other platform settings.


  2. Alex D 16 Reputation points
    2024-03-18T09:02:08.34+00:00

    Apologies, I found out it was a mistake I made in the Build Configuration.

    I have a separate project for unit tests that I added to the solution, this project had a separate appsettings.json file that was overwriting the main project.

    While at first I thought I excluded the test project from the build, I only did so for the "Debug" configuration, not the "Release" hence the different result on Azure.