Azure Web Apps GitHub部署时web.config转换及连接字符串问题咨询
Absolutely! Web.config transformations are fully supported in Azure Web Apps when using GitHub deployment, and they’re a perfect solution for resolving your post-deployment connection errors with Umbraco. Let’s break down how to implement this properly:
1. Add Transformation Files to Your Umbraco Project
First, make sure you have the right config transformation files in your project. Visual Studio usually creates Web.Debug.config and Web.Release.config by default, but you can create a dedicated one for Azure (like Web.Azure.config) if you prefer:
- Right-click your root
web.configin Visual Studio → Add Config Transformation - Rename the new file to
Web.Azure.configto keep your environments organized
2. Define the Connection String Transformation
Umbraco relies on the umbracoDbDSN connection string for its database. In your transformation file (e.g., Web.Azure.config), add this code to overwrite the local connection string with your Azure SQL details:
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="umbracoDbDSN" connectionString="Server=tcp:YOUR_SERVER_NAME.database.windows.net,1433;Initial Catalog=YOUR_DB_NAME;Persist Security Info=False;User ID=YOUR_DB_USER;Password=YOUR_DB_PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> </connectionStrings> </configuration>
- The
xdt:Locator="Match(name)"targets the exact connection string by its name, andxdt:Transform="SetAttributes"overwrites its values with your Azure-specific details.
3. Tell Azure to Apply the Transformation During Deployment
There are two straightforward ways to trigger the transformation when deploying from GitHub:
Option 1: Use Azure App Settings (Simplest)
- Navigate to your Azure Web App → Configuration → Application settings
- Add a new setting with:
- Key:
SCM_BUILD_ARGS - Value:
/p:TransformConfigFiles=true /p:Configuration=Azure
Note: ReplaceAzurewithReleaseif you’re usingWeb.Release.configinstead of a customWeb.Azure.config.
- Key:
Option 2: Custom Kudu Deployment Script (More Control)
If you want fine-grained control over the deployment process, generate a custom Kudu script:
- Run this command in your project directory (use Azure CLI locally or in Cloud Shell):
az webapp deployment script --resource-group YOUR_RESOURCE_GROUP --name YOUR_WEB_APP --script-type bash --output-path .deployments/deploy.sh - Open the generated
deploy.shfile, find theMSBuildcommand, and add the transformation arguments:MSBuild "$DEPLOYMENT_SOURCE/YourUmbracoProject.csproj" /nologo /verbosity:m /t:Build /t:pipelinePreDeployCopyAllFilesToOneFolder /p:_PackageTempDir="$DEPLOYMENT_TEMP" /p:AutoParameterizationWebConfigConnectionStrings=false /p:TransformConfigFiles=true /p:Configuration=Azure - Commit this script to your GitHub branch so Azure uses it during deployment.
4. Verify the Transformation Worked
After deploying, you can confirm the connection string was updated correctly:
- Go to your Web App → Advanced Tools → Go (opens the Kudu portal)
- Navigate to
site/wwwrootand open theweb.configfile. Check that theumbracoDbDSNmatches what you defined in the transformation file.
Alternative: Use Azure App Settings to Override Connection Strings
For an even simpler approach (and better security, since you won’t commit sensitive credentials to GitHub), you can override the connection string directly in Azure:
- Go to your Web App → Configuration → Connection strings
- Add a new connection string with:
- Name:
umbracoDbDSN - Value: Your Azure SQL connection string
- Type:
SQLAzure
Azure will automatically replace the connection string in yourweb.configat runtime—no transformations needed!
- Name:
内容的提问来源于stack exchange,提问作者Eric Shay

