4

我有一组应用程序从 IIS 根目录中的 web.config 继承其连接字符串。这意味着我不需要在应用程序的根目录中指定连接字符串,但我在本地调试时确实需要指定连接字符串。

我的问题是,如何在 web.debug.config 中设置仅在调试时使用的连接字符串?

4

2 回答 2

5

You could specify the connectionstring in the normal web.config and then in the web.release.config add a transform that simply removes the connection string all together.

That way it should exist in the debug one but not in the web.release.config.

Of course this assumes you are using those configs with transforms and not doing a simple copy/paste of the code when you deploy

MSDN has a good example of this

The following example shows how to select all the connection string add elements in the development Web.config file. In the deployed Web.config file, only the first connection string element is removed.

<configuration xmlns:xdt="...">
  <connectionStrings>
    <add xdt:Transform="Remove" />
  </connectionStrings>
</configuration>

EDIT: I guess alternatively you could also create a transform in web.debug.config that adds it while in debugging, which might help to keep it out of the original web.config if you aren't appying transforms when deploying

于 2012-09-04T22:26:23.380 回答
0

将您的调试连接字符串保留在 Web.Config 中,并使用定位器替换 Web.Release.Config 中的发布连接字符串,如下所示 <connectionStrings> <add name="DefaultConnection" connectionString="Release Connections tring" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/connectionStrings)"/> </connectionStrings>

于 2016-01-05T08:29:41.593 回答