C# Can';无法从web.config读取连接字符串
我在我的C# Can';无法从web.config读取连接字符串,c#,asp.net-core,web-config,C#,Asp.net Core,Web Config,我在我的web.config项目根目录中有以下内容: <configuration> <connectionStrings> <clear /> <add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connectio
web.config
项目根目录中有以下内容:
<configuration>
<connectionStrings>
<clear />
<add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connection Timeout=3000;" />
</connectionStrings>
<appSettings>
<add key="ConnectionString" value="test"/>
</appSettings>
....
但是,当我中断此操作时,ConfigurationManager.ConnectionStrings
和ConfigurationManager.AppSettings
为空(第一个默认连接字符串不是web.config
中的字符串)
这是怎么回事?您必须将配置迁移到新文件appsettings.json
您必须将配置迁移到新文件appsettings.json
不要说这是你应该做的,但你可以做到以下几点 在ASP.Net Core 2.2中,您应该能够使用将XML配置添加到IConfigurationBuilder
configBuilder.AddXmlFile("app.config");
内容和上面差不多
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
不要说这是你应该做的,但你可以做以下 在ASP.Net Core 2.2中,您应该能够使用将XML配置添加到IConfigurationBuilder
configBuilder.AddXmlFile("app.config");
内容和上面差不多
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
asp.net-core不使用configuration manager,也不从web.config读取。这些设置存储在appsettings.json.asp.net-core中,不使用configuration manager,也不从web.config读取。这些设置存储在appsettings.json中。