gpt4 book ai didi

asp.net-mvc - 合并后从外部文件转换 appSettings

转载 作者:行者123 更新时间:2023-12-02 09:44:12 25 4
gpt4 key购买 nike

我想做的是 transform 外部文件中的 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>

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>

在正确的配置中构建之后,在我的示例中是调试,只有这样:

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

我尝试通过 2 个或多个不同的 project 共享 appSettings 第一个是 WCF Service 第二个 ASP.NET MVC 4 应用程序

已编辑:

我尝试将此文件 属性移动到Web.Debug.config,但它也不起作用。

问题是:

我怎样才能完成这样的事情?这可能吗?

最佳答案

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

Please notice the added section, you have to add it into cs project file in a TEXT editor manually. Please replace ISWB with yours. And then save it.

它应该工作得很好。享受它吧!

关于asp.net-mvc - 合并后从外部文件转换 appSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22509321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com