C
C#2w ago
Salman

✅ How to setup environment variables in Azure Web App

I've deployed my Blazor Server App to Azure Web App. And in App settings I've added some items e.g my Cloudinary URL and in Connection Strings section I've made a connection string named as "DefaultConnection" same as it was in my appsettings.json file. However I'm getting some Postgress EFCore error in production. Here's my Program.cs: https://paste.mod.gg/ltvubhnjcema/0 My appsettings.json structure:
{
"ConnectionStrings": {
"DefaultConnection": ""
},
"AuthMessageSenderOptions": {
"SendGridKey": "",
"SendGridDomain": ""
},
"CloudinaryUrl": "",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
{
"ConnectionStrings": {
"DefaultConnection": ""
},
"AuthMessageSenderOptions": {
"SendGridKey": "",
"SendGridDomain": ""
},
"CloudinaryUrl": "",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
AI told me that the environment variables I set are supposed to override these values but apparently they aren't and I'm not sure how to check. Can someone guide me the correct way to do it. Thanks
12 Replies
Sossenbinder
Sossenbinder2w ago
How did you define the env variables? There's a common issue with separators To quote from the docs:
In a default Linux app service or a custom Linux container, any nested JSON key structure in the app setting name needs to be configured differently for the key name. Replace any colon (:) with a double underscore (). Replace any period (.) with a single underscore (_). For example, ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsightsInstrumentationKey for the key name.
Salman
SalmanOP2w ago
This is how Envs are defined
No description
Salman
SalmanOP2w ago
Connection String
No description
Sossenbinder
Sossenbinder2w ago
Configure an App Service App - Azure App Service
Learn how to configure common settings for an Azure App Service app. You can use the Azure portal, Azure CLI, or Azure PowerShell.
Sossenbinder
Sossenbinder2w ago
This states the connection strings are accessible with a specific prefix at runtime To be fair I never really used the connection strings config option for azure web apps I just use env vars, I don't like the implicit conventions for the most part
Salman
SalmanOP2w ago
Oh thanks for sharing that guide, I'll try it tomorrow and will let ya know That prefix thing might do it , hopefully
Teddy
Teddy2w ago
you had it correct,
{
"ConnectionStrings": {
"DefaultConnection": ""
}
}
{
"ConnectionStrings": {
"DefaultConnection": ""
}
}
would look like the screenshot in Azure
No description
Teddy
Teddy2w ago
I do get weird issues by choosing a specific connection string type though often, so usually just choose Custom as the type
Salman
SalmanOP2w ago
I'll try custom type. How do you Access it in your Program.cs? Just like normal or with prefix
Teddy
Teddy2w ago
No description
Teddy
Teddy2w ago
builder.Configuration.GetConnectionString("DefaultConnection")
Salman
SalmanOP2w ago
Alright, thanks @Zaneris @Sossenbinder tysm, I just changed my connection string type to "Custom" and apparently it has started working. God bless ya

Did you find this page helpful?