c# - Can't read connection string from web.config
问题描述
I have the following in my web.config
located at the root of my project:
<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>
....
I read from Startup.cs
(this is an asp.net core web app):
string connection = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
However when I break on this, ConfigurationManager.ConnectionStrings
and ConfigurationManager.AppSettings
are empty (well, the first has some default connection string that is not the one in web.config
).
What's going on here?
解决方案
您必须将配置迁移到新文件 appsettings.json https://docs.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1