是否有一种本机方式可以动态更改构建中的web.config设置?

时间:2017-07-19 13:00:31

标签: c# asp.net

我们正在使用一个库,该库需要在每次构建时在web.config中更新一个值。有没有办法本地做到这一点?

我们正在使用web.config

中具有自定义配置部分的库

每次构建都需要更新版本属性。增量甚至DateTime.Now等效都可以。

<clientDependency version="6">
...
</clientDependency>

3 个答案:

答案 0 :(得分:2)

您可以尝试使用MSBuild Tasks

也许XMLPoke使用格式化的日期/时间字符串更新web.config

<Target Name="EchoTime">
    <Time Format="yyyyMMddHHmmss">
        <Output TaskParameter="FormattedTime" PropertyName="currentTime" />
    </Time>
    <Message Text = "$(currentTime)" />
</Target>

<Target Name="UpdateWebConfig">
  <XmlPoke 
      XmlInputPath="web.config"
      Query="//<complete-path>/clientDependency/@version"
      Value="$(currentTime)" />
</Target>

使用configSource可更新部分移动到外部文件也可以使工作更轻松。

答案 1 :(得分:1)

您应首先查看为什么每次构建都会更改配置。这似乎并不好。也许你可以改变它。

但是,在Visual Studio 2013中引入了Gulp任务运行器。

你可以稍微滥用它。

Basicly:

答案 2 :(得分:-2)

这是与MSBuild中的XmlPoke有关的其他示例。

发布时自动用yyyyMMddmmss替换VersionBuild值

Web.config
<appSettings>
    <add key="VersionBuild" value=".121"/>
</appSettins>
<Target Name="BeforeBuild">
    <PropertyGroup>
      <CurrentDate>$([System.DateTime]::Now.ToString(yyyyMMddmmss))</CurrentDate>
    </PropertyGroup>
    <XmlPoke 
      Condition="'$(PublishProfileName)' != ''"
      XmlInputPath="Web.config"
      Query="/configuration/appSettings/add[@key = 'VersionBuild']/@value"
      Value=".$(CurrentDate)" />
</Target>
相关问题