已经有similar topic,但它没有回答我的问题。
在visual studio 2010中开发Web应用程序时,您可以拥有多个配置文件。您有默认的Web.config,然后您可以拥有Web.Debug.config和Web.Release.config。
现在让我们构建一个C#控制台应用程序并将其命名为Foo。默认配置文件应为Foo.exe.Config。 我们可以为不同的环境覆盖此配置文件吗? 我们该如何称呼它? Foo.exe.Release.config或Foo.Release.exe.config?
答案 0 :(得分:4)
查看此帖子: http://www.olegsych.com/2010/12/config-file-transformation/ 演示如何针对不同的环境使用不同的配置。
答案 1 :(得分:3)
VS2010中没有本机支持,但您可以查看此博客: http://vishaljoshi.blogspot.com/2010/05/applying-xdt-magic-to-appconfig.html
答案 2 :(得分:2)
本文以8个步骤解释如何执行此操作:https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
它解决了我的问题。
更新:如果文章被移除(thx @ A.V :))
,我在下面添加步骤准备项目并添加 app.config , app.debug.config 和 app.release.config 。确保在 .Net 4.0 下运行。
右键单击该项目,单击“卸载项目”,然后单击“编辑.csproj”。
在最后一个 PropertyGroup 下方添加以下内容:
<PropertyGroup> <ProjectConfigFileName>App.config</ProjectConfigFileName> </PropertyGroup>
<ItemGroup> <None Include="App.config" /> <None Include="App.Debug.config"> <DependentUpon>App.config</DependentUpon> </None> <None Include="App.Release.config"> <DependentUpon>App.config</DependentUpon> </None> </ItemGroup>
&LT;进口 项目=&#34; $(MSBuildExtensionsPath)\微软\ VisualStudio的\ V10.0 \网络\ Microsoft.Web.Publishing.targets&#34; /&GT;
<Target Name="AfterBuild"> <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)"
目的地=&#34; @(AppConfigWithTargetPath-&GT;&#39; $(OUTDIR)%(TARGETPATH)&#39;)&#34; /&GT;
现在您可以保存项目,右键单击项目并选择Reload Project。
对于app.debug.config / app.release.config文件,您可以使用为Web项目提供的模板,如下所示
<?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="Mode" value="Debug" xdt:Transform="Insert"/> </appSettings> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration>