ConfigurationManager 是否与 ASP.NET core 的 appsettings.json 一起使用? [英] Does ConfigurationManager work with ASP.NET core's appsettings.json?

查看:17
本文介绍了ConfigurationManager 是否与 ASP.NET core 的 appsettings.json 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Standard 库,其中包含我所有的 SQL 相关代码.它甚至包含一些创建 SQL 连接的代码.该库需要从应用程序配置文件中读取以获取 SQL 连接字符串.该库使用典型的ConfigurationManager.ConnectionStrings 方法.

I have a .NET Standard library that contains all of my SQL related code. It even has some code in it that creates the SQL connection. This library needs to read from the application config file in order to grab the SQL connection string. The library is using the typical ConfigurationManager.ConnectionStrings approach.

现在,我在 .NET Core ASP.NET Web Api 2 应用程序中使用了这个库.我在这个应用程序的 appsettings.json 文件中定义了我的连接字符串.连接字符串位于 ConnectionStrings 字段中,其给定名称与上面的 DLL 查找的名称相匹配.

Now, I use this library in a .NET Core ASP.NET Web Api 2 application. I've got my connection string defined in this application's appsettings.json file. The connection string is in the ConnectionStrings field with a given name that matches the name that my DLL from above looks for.

这似乎不起作用.我的 DLL,从顶部开始,没有从配置文件中找到连接字符串.

This does not appear to work. My DLL, from the top section, does not find the connection string from the config file.

ConfigurationManager 不能与 appsettings.json 一起使用吗?如果没有,我应该如何处理?

Does ConfigurationManager not work with appsettings.json? If not, how should I approach this?

推荐答案

ConfigurationManager 不适用于 appsettings.json,您必须使用 ConfigurationBuilder 类代替 ConfigurationManager 在 startup.cs 文件中添加 json 文件.

ConfigurationManager does not work with appsettings.json, instead of ConfigurationManager you have to use ConfigurationBuilder class for add json file in startup.cs file.

1.将下面的代码放在appsettings.json中

1.Put below code in appsettings.json

"ConnectionStrings": {
 "connectionstring ": "Data Source=Demo_server\SQLEXPRESS01;Initial Catalog=Demo_DB;Persist Security Info=True; User ID=sa;Password=Password@123" }

2.将下面的代码放在startup.cs中.

2.Put below code in startup.cs.

 public Startup(IHostingEnvironment env)
    {
         Configuration = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();             
    }

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {            
       ConnectionString = Configuration["ConnectionStrings:connectionstring"]; // Get Connection String from Appsetting.json
    }

3.创建 ConnectionStringUtility 类以从 Startup.cs 文件中获取连接字符串.

3.Create ConnectionStringUtility class for get connection string from Startup.cs file.

public class ConnectionStringUtility
{ 
  public static string GetConnectionString()
  { 
    return Startup.ConnectionString; 
  } 
}

4.在连接变量中获取连接字符串并在任何你想要的地方使用这个连接字符串.

4.Get Connectionstring in connection variable and use this connection string where ever you want.

public string Connectionstring = ConnectionStringUtility.GetConnectionString();

这篇关于ConfigurationManager 是否与 ASP.NET core 的 appsettings.json 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆