防止Web部署上的web.config更改

时间:2015-08-19 15:50:47

标签: c# web-deployment

我们有一个用C#开发的应用程序的Web部署包,当安装在IIS中时,web.config中有几个设置,例如:

<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>

可能会在部署的每个站点上更改设置(例如上面的ReportFiles),然后如果有应用程序更新,我们将安装最新的Web部署包。

不幸的是,这会覆盖可能已更改的所有设置,恢复为默认设置。这意味着每次我们更新应用程序时,我们都必须获取web.config的副本,然后执行更新,然后将其复制回来。

有没有办法在创建后停止更新web.config?或者在Web部署期间允许安装程序查看现有设置并决定是否保留?

5 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

我从某个我无法记住的地方偷走了这个。将它放在一个新的blah.targets文件中,并将<Import Project="blah.targets" />添加到<Project>下某处的csproj文件中。它将使webdeploy不会覆盖现有的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>

答案 2 :(得分:1)

这些答案更正确且更可靠,但是,如果您想从Visual Studio中进行快速而肮脏的Web部署并跳过web.config或其他文件,只需先单击“预览”按钮,然后取消选中您不使用的文件;在点击“发布”之前不想从列表中获取。

答案 3 :(得分:0)

将“构建操作”从“内容”更改为“无”,并确保将“复制到目录”设置为“不复制”。对于appsettings.json

也是如此

答案 4 :(得分:-1)

我认为你应该用数据库来做这件事。根据某些ID(如客户端ID或任何内容)将报表文件保存在表中,并从那里开始使用。

相关问题