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

查看:44
本文介绍了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,而不是ConfigurationManager,您必须使用ConfigurationBuilder类在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.在连接变量中获取Connectionstring并在需要的地方使用此连接字符串.

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天全站免登陆