Web.Config 调试/发布

91

我知道在Visual Studio 2010中,web.config提供了从Debug模式切换到Release模式的数据库能力。

这是我的Web.Release.config:

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

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>

</configuration>

这是我的 Web.Debug.config 代码:

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

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Live1" connectionString="Data Source=Live;Initial Catalog=LiveDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>

</configuration>

这是我的Web.config代码:

<?xml version="1.0"?>

<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
       <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
       <providers>
          <clear/>
          <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
       </providers>
    </membership>

    <profile>
       <providers>
          <clear/>
          <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
       </providers>
    </profile>

    <roleManager enabled="false">
       <providers>
          <clear/>
          <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
       </providers>
    </roleManager>

  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我发布我的项目时,Web.config文件中没有任何内容。我的Live数据库连接字符串没有显示出来?

4个回答

145

Visual Studio 2010中的web.config转换使用XSLT来将当前的web.config文件转换为其.Debug或.Release版本。

在您的.Debug/.Release文件中,您需要在连接字符串字段中添加以下参数:

xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
这将导致每个连接字符串行查找匹配的名称并相应地更新属性。 注意:您无需担心在转换文件中更新providerName参数,因为它们不会更改。 以下是我其中一个应用程序的示例。下面是web.config文件的部分内容:
<connectionStrings>
      <add name="EAF" connectionString="[Test Connection String]" />
</connectionString>

这里是web.config.release部分进行正确转换的代码:

<connectionStrings>
      <add name="EAF" connectionString="[Prod Connection String]"
           xdt:Transform="SetAttributes"
           xdt:Locator="Match(name)" />
</connectionStrings>
注意:转换只会在发布网站时发生,而不是在使用 F5 或 CTRL + F5 运行它时发生。如果您需要在本地对特定配置运行更新,则必须手动更改 Web.config 文件。更多细节请参阅 MSDN 文档:https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

23
你能在回答中加上一个注释,说明这是在发布时间而不是F5更新的时间发生的吗?显然我已经正确了两个小时,但没有意识到这一点。 - Paul
我该如何在Visual Studio Online Continuous Build中使其工作?我希望它能在构建和部署到Azure期间转换我的Web.Config。 - Rosdi Kasim
1
@RosdiKasim - 我不确定这是否完全适用于Visual Studio在线版,但当我需要部署特定项目(如果我有多个)或使用不同的构建(因此需要转换)时,我直接在Azure网站实例中指定。这是我之前写的一些详细信息:http://www.freshconsulting.com/2-alternate-configurations-for-windows-azure-deployment/ - Dillie-O
这是一个链接到 MSDN 的页面,详细描述了此内容 https://msdn.microsoft.com/zh-cn/library/dd465326(VS.100).aspx - Hakan Fıstık
1
你关于通过F5在VS中运行网站与发布的附加说明对我非常有帮助。 - Denis M. Kitchen
显示剩余5条评论

9

可以使用Nuget包中提供的ConfigTransform构建目标进行转换 - https://www.nuget.org/packages/CodeAssassin.ConfigTransform/

所有的 "web.*.config" 转换文件将被转换并输出为一系列的 "web.*.config.transformed" 文件,无论选择哪种构建配置都会在构建输出目录中。

非 web 项目中的 "app.*.config" 转换文件也是同样的道理。

然后将以下目标添加到您的*.csproj中。

<Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" >
    <TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" />
</Target>

作为第一个在谷歌上出现的关于此主题的Stackoverflow帖子,发表一个答案。

1
这个方法非常好,对我来说完美地运行了。然而,我不需要安装你提到的 ConfigTransform NuGet(它会始终转换所有配置)。我只是将你答案中的目标片段复制到了 csproj 中。然后当我在 Visual Studio 上设置活动构建配置(例如,将其更改为调试),在构建解决方案时就会相应地进行转换。 - BornToCode

5
为了使转换在开发中起作用(使用F5或CTRL + F5),我将ctt.exe(https://ctt.codeplex.com/)放入packages文件夹中(packages \ ConfigTransform \ ctt.exe)。
然后我在Visual Studio中注册了一个预建事件或后建事件...
$(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)connectionStrings.config" transform:"$(ProjectDir)connectionStrings.$(ConfigurationName).config" destination:"$(ProjectDir)connectionStrings.config"
$(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)web.config" transform:"$(ProjectDir)web.$(ConfigurationName).config" destination:"$(ProjectDir)web.config"

对于变换,我使用SlowCheeta VS扩展(https://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5)。


1
为了保留空格(防止转换后的配置在一行上),请在命令行中添加 preservewhitespace indent IndentChars:" "。$(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)connectionStrings.config" transform:"$(ProjectDir)connectionStrings.$(ConfigurationName).config" destination:"$(ProjectDir)connectionStrings.config" preservewhitespace indent IndentChars:" " - Emanuel Nilsson

4
如果你要用新的连接字符串替换所有的连接字符串以供生产环境使用,你可以使用以下语法简单地将所有连接字符串替换为生产环境中的连接字符串:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<connectionStrings xdt:Transform="Replace">
    <!-- production environment config --->
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
</connectionStrings>
....

本答案的信息来自于这个回答这篇博客文章

注意:正如其他人已经解释过的那样,在应用程序发布时才会应用此设置,而不是通过按下F5运行/调试应用程序时。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接