[英]ASP.NET and Visual Studio 2019: how to set up Web.Debug and Web.Release if you have configSource in Web.config?
在我的 ASP.NET web 表单中,这是我的 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>
但是,这引用了 Web.config 文件中的<connectionStrings>
部分,这是我一开始没有的,因为在我的项目中我有:
<connectionStrings configSource="MySecrets.config"
如何设置 Web.Release 以替换MySecrets.config
文件,使其在发布后不可见?
使用预处理器在连接字符串之间切换,为此您应该同时拥有两个连接字符串。
web.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# 参考)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.