We have a web deployment package for an app developed in C#, when installed in IIS the web.config has several settings in it, for example:
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="ReportFiles" value="http://localhost/ReportTemp/"/>
</appSettings>
A setting may get changed (for example ReportFiles above) at each site it is deployed and then if there is an update to the app we would install the latest web deployment package.
Unfortunately this overwrites all the settings that may have changed, back to the default. This means every time we update the application we have to take a copy of the web.config, do the update then copy it back.
Is there a way of stopping the web.config being updated once created? Or during the web deployment allowing the installer to see the existing settings and decide whether to keep?
Take a look at Web.config transforms
https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
or this
How do I prevent 'Publish Web' from overwriting config files?
I stole this from somewhere I can't remember. Put this in a new blah.targets file and add <Import Project="blah.targets" />
to your csproj file somewhere under <Project>
. It will make webdeploy not overwrite the existing web.config.
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--These UseMsDeployExe and AfterAddIisSettingAndFileContentsToSourceManifest are relevant to the msdeploy, perhaps otherwise in the pubxml files-->
<PropertyGroup>
<UseMsDeployExe>true</UseMsDeployExe>
<AfterAddIisSettingAndFileContentsToSourceManifest>OnAfterAddIisSettingAndFileContentsToSourceManifest</AfterAddIisSettingAndFileContentsToSourceManifest>
</PropertyGroup>
<!-- We do not want to OVERWRITE the web.config file if it is present at the destination -->
<!-- We also don't want to delete it so we can't just exclude it -->
<Target Name="ExcludeWebConfig">
<Message Text="Excluding Web Config From Publish. Be sure to manually deploy any new settings." Importance="high" />
<ItemGroup>
<MsDeploySkipRules Include="SkipWebConfigDelete">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>$(_DestinationContentPath)\\Web.config</AbsolutePath>
<Apply>Destination</Apply>
</MsDeploySkipRules>
<MsDeploySkipRules Include="SkipWebConfigUpdate">
<SkipAction>Update</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>$(_DestinationContentPath)\\Web.config</AbsolutePath>
<Apply>Destination</Apply>
</MsDeploySkipRules>
</ItemGroup>
</Target>
<Target Name="OnAfterAddIisSettingAndFileContentsToSourceManifest" DependsOnTargets="ExcludeWebConfig" />
</Project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With