asp.net - 无法弄清楚 VS2010 中的 web.debug.config 与 web.config 替代

标签 asp.net web-config

有人可以指出我在这里做错了什么吗?

我正在尝试设置一个 ASP.NET Web 应用程序项目,以使用两个版本的 web.config 文件进行编译,以进行发布和调试版本。因此,为了简单起见,这是我的 web.config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="WhyMicrosoftSucksSoMuch" connectionString="" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

然后我在 web.debug.config 中执行以下操作:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
      <add name="WhyMicrosoftSucksSoMuch"
           connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\FilePath\Database1.mdf';User Instance=true"
           providerName="System.Data.SqlClient" 
           xdt:Transform="SetAttributes" xdt:Locator="Match(connectionString)" />
  </connectionStrings>
</configuration>

如果我在调试配置下发布它,生成的 web.config 看起来不错,但是当我尝试在调试配置下从 VS2010 运行我的项目时,当我的逻辑尝试访问数据库时,我会收到错误:

The ConnectionString property has not been initialized.

那么这里有什么技巧呢?

PS。请不要将我指向 this document 。我试着读了好几次,但太多多余的信息让我头疼。我猜 MS 不知道brief是什么。

最佳答案

在 Debug模式下运行它时,它不会应用任何转换。

它仅在发布期间应用它们。您可以将调试连接字符串放在主 web.config 中,并将生产连接字符串添加到 web.release.config

此外,您可能会想使用

xdt:Transform="Replace"

关于asp.net - 无法弄清楚 VS2010 中的 web.debug.config 与 web.config 替代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387232/

相关文章:

php - Azure使用php上传大文件

asp.net - 如何在 IIS7 上全局将enableVersionHeader设置为 false

c# - 如何摆脱 Web.config 中的单元测试遗留问题?

mysql - 使用内连接在gridview中绑定(bind)数据

asp.net - 谷歌地图使用多边形突出显示带边界的国家/地区

c# - 如何访问和/或修改代码隐藏 (ASP.net c#) 中的 (sql)DataSource?

mysql - 如何在Mysql数据库中使用Asp Net.Identity core

c# - webconfig中的属性maxRequestLength是怎么测的

asp.net - 防止 FormsAuthenticationModule 拦截 ASP.NET Web API 响应

web.config 文件中的 C# 连接字符串