gpt4 book ai didi

c# - ASP.NET 和 Visual Studio 2019 : how to set up Web. 调试和 Web.Release 如果您在 Web.config 中有 configSource?

转载 作者:行者123 更新时间:2023-11-30 22:51:39 25 4
gpt4 key购买 nike

在我的 ASP.NET 网络表单中,这是我的连接字符串在我的 Web.config 文件中的样子:

<connectionStrings configSource="MySecrets.config"/>

我知道我可以使用 Web.Debug 和 Web.Release 来更改连接字符串,这样它们就不会在发布 Web 应用程序时公开。

但是Visual Studio提供的例子中提到:

In the example below, the "SetAttributes" transform will change the value of 
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "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>

然而,这引用了一个 <connectionStrings>我首先没有的 Web.config 文件中的部分,因为在我的项目中我有:

<connectionStrings configSource="MySecrets.config"

如何设置 Web.Release 来替换 MySecrets.config文件以便在发布后不可见?

最佳答案

使用预处理器在连接字符串之间切换,为此你应该同时拥有两个连接字符串。

网络配置

<connectionStrings>
<add name="Project.Properties.Settings.ConnString_A" connectionString="" providerName="System.Data.SqlClient" />
<add name="Project.Properties.Settings.ConnString_B" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>

代码隐藏

public SqlConnection conn { get; set; }
public DbContext()
{
#if DEBUG
conn = new SqlConnection(Properties.Settings.Default.ConnString_A);
#else
conn = new SqlConnection(Properties.Settings.Default.ConnString_B);
#endif
}

引用:#if (C# Reference)

关于c# - ASP.NET 和 Visual Studio 2019 : how to set up Web. 调试和 Web.Release 如果您在 Web.config 中有 configSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58959362/

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