Hiren Amin
Hiren Amin

Reputation: 77

Prevent web.config change on web deployment

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?

Upvotes: 5

Views: 6564

Answers (5)

realPro
realPro

Reputation: 1846

Change Build Action from Content to None and make sure that the Copy to Dir.. is set to Do not copy. This is true also for appsettings.json

Upvotes: 0

These answers are more correct and futureproof, however if you want to do a quick and dirty web deploy from visual studio and skip web.config or another file, simply hit the 'preview' button first and then uncheck the files you don;t want from the list before hitting publish.

Upvotes: 1

Mohit Saxena
Mohit Saxena

Reputation: 1449

I think you should do this with database. Keep your report files in a table based on some id (like client Id or anything) and use from there.

Upvotes: 0

Jason Kleban
Jason Kleban

Reputation: 20758

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>

Upvotes: 1

Related Questions