在ASP.Net MVC Core项目中,连接字符串存在于appsettings.json
文件中。我还有一个与该解决方案相同的类库项目,我想从那里获取Web项目的连接字符串,但是我在官方资源中找不到帮助,我该如何实现?
更新:类库是.Net 4.6.1,同时在ASP.Net Core项目中我正在使用.NET 4.6.1。
在ASP.Net MVC Core项目中,连接字符串存在于appsettings.json
文件中。我还有一个与该解决方案相同的类库项目,我想从那里获取Web项目的连接字符串,但是我在官方资源中找不到帮助,我该如何实现?
更新:类库是.Net 4.6.1,同时在ASP.Net Core项目中我正在使用.NET 4.6.1。
在 Startup.cs
中注册 Configuration
public void ConfigureServices(IServicesCollection services)
{
services.AddSingleton(Configuration);
}
public HomeController(IConfigurationRoot Configuration)
{
this.Configuration= Configuration;
}
然后您可以像在Startup.cs
中一样获取连接字符串
Configuration.GetConnectionString("DefaultConnection")
System.Configuration
。 - AbhayIOptions <T>
模式。 - Tseng