2014-03-19 10 views
2

后,我试图做的是appSettingstransform一个是在外部文件:变换从外部文件的appSettings合并

下面是external.config

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

的Web.config

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

网络。 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> 

我tryed作出共享appSettings由2个或更多不同project 1阶为WCF ServiceASP.NET MVC 4Application

编辑:

我tryed迁此fileattributeWeb.Debug.config但它也不起作用。

的问题是:

我怎么能完成这样的事情是它甚至有可能?

+0

您在上面找到的任何解决方案?谢谢 – sairfan

回答

0

有趣。我有和你一样的问题。所以现在这里是一个解决方法供您参考。 请打开项目文件 - 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> 

请注意添加的部分,你必须将其添加到手动在TEXT编辑器中编辑项目文件。 请把ISWB换成你的。然后保存它。

它应该很好。 享受它!