4

我想做的是transform其中之一appSettings在外部文件中:

这是external.config

<?xml version="1.0"?>
    <appSettings>
    <add key="SomeKey" value="some value" />
    </appSettings>

网页配置

<?xml version="1.0"?>
    <configuration>
        <appSettings file="..\..\external.config">
            <add key="SomeKey1" value="some value 1" />
        </appSettings>
    </configuration>

Web.Debug.config

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
            <add key="SomeKey" value="some changed value"xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
       </appSettings>
    </configuration>

在正确构建之后configuration,在我的示例中Debug只有这个:

<?xml version="1.0"?>
    <configuration>
        <appSettings file="..\..\external.config">
            <add key="SomeKey1" value="some value 1" />
        </appSettings>
    </configuration>

但应该是:

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
            <add key="SomeKey1" value="some value 1" />
            <add key="SomeKey" value="some changed value"/>
       </appSettings>
    </configuration>

我试图让appSettings2 个或更多不同project的 1-st共享WCF Service第二个ASP.NET MVC 4 Application

编辑:

我试图将其移至file attributeWeb.Debug.config但它也不起作用。

问题是:

我怎么能完成这样的事情?甚至可能吗?

4

1 回答 1

1

有趣的。我有和你一样的问题。所以现在这里有一个解决方法供您参考。请打开项目文件 - XXX.csproj 例如 ISWB.Test.Unit.csproj

像这样添加下面的部分

<!-- Rock Add here, 2015.03.19 enable the external config transformation -->
  <Target Name="BeforeCompile" Condition="Exists('ISWB.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="ISWB.config" Destination="$(IntermediateOutputPath)ISWB.config" Transform="ISWB.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="ISWB.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)ISWB.config">
        <TargetPath>ISWB.config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

  <Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

请注意添加的部分,您必须手动将其添加到文本编辑器中的 cs 项目文件中。请用您的替换 ISWB。然后保存它。

它应该运作良好。好好享受!

于 2015-03-19T06:24:57.987 回答