C# 无法通过ASP.Net核心连接到SQL Server(始终返回空值)

C# 无法通过ASP.Net核心连接到SQL Server(始终返回空值),c#,angular,asp.net-core,sql-server-2016,C#,Angular,Asp.net Core,Sql Server 2016,我正在尝试通过n层体系结构设计通过Configuration Manager连接到本地SQL Server。简言之,我想从本地数据库查询数据,将其从数据集序列化为JSON,并以JSON格式读取数据,以便从我的角度聆听数据 我在一个控制台应用程序中实现了我的n-tier,并测试了它,结果是它应该是这样的,但将确切的n-tier放入我的ASP.NET核心构建中,它不断返回 System.Configuration.ConnectionStringSettingsCollection.this[str

我正在尝试通过n层体系结构设计通过Configuration Manager连接到本地SQL Server。简言之,我想从本地数据库查询数据,将其从数据集序列化为JSON,并以JSON格式读取数据,以便从我的角度聆听数据

我在一个控制台应用程序中实现了我的n-tier,并测试了它,结果是它应该是这样的,但将确切的n-tier放入我的ASP.NET核心构建中,它不断返回

System.Configuration.ConnectionStringSettingsCollection.this[string]。get返回null

起初我实现了web.config,因为我不知道
appsettings.json
,但当我试图通过GET从该json文件调用
ConnectionString
时,它总是返回null(可以是任何内容)

在BLL类中,我尝试删除控制器继承,但是没有显示任何值

我还尝试了整个
Startup
方法,但这需要太多依赖项

文件夹结构:

Controller -> N-Tier -> DBConn;DAL;BLL;
appsettings.json

"ConnectionStrings": 
  { "Local_One": "Data source=localhost;Initial Catalog=VAS; User ID=user101; Password=password1;Trusted_Connection=True;MultipleActiveResultSets=true;" },


public class DBConn
{
    public string ConnectionString()
    {
        //Also where the error gets caught
        return ConfigurationManager.ConnectionStrings["Local_One"].ConnectionString;
    }
}
公共类DAL

public class DAL
{
    SqlConnection conn = new SqlConnection(new DBConn().ConnectionString());
    public string query = null;
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    int i = 0;

    public DataSet GetAllUserTypes()
    {
        if(conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        try
        {
            query = "sp_GETUSERTYPES"; //calls a stored procedure
            da.SelectCommand = new SqlCommand(query,conn);
            da.Fill(ds);
            ds.Tables[0].TableName = "USER TYPES";
            da.Dispose();
            conn.Close();
        }
        catch(SqlException e)
        {
        }

        return ds;
    }
}
公共类BLL

[Produces("application/json")]
[Route("api/N-Tier/BLL/[action]")]//localhost:5000/api/N-Tier/BLL/GetAllUserTypes
public class BLL:Controller
{
    DAL dal = new DAL();

    public string GetAllUserTypes()
    {
        return JsonConvert.SerializeObject(dal.GetAllUserTypes(),Formatting.Indented);//using Newtonsoft.Json;
    }
}
预期结果(我从控制台App.config获得):

错误消息:

System.Configuration.ConnectionStringSettingsCollection.this[string]。get返回null


在asp.net Core中,您不应该使用ConfigurationManager从appsettings.json中删除,这就是为什么会得到null。相反:

在Startup.cs中

public void ConfigureServices(IServiceCollection services)
{
    ...
    // Add the whole configuration object here.
    services.AddSingleton<IConfiguration>(Configuration);
}
现在,稍后在视图代码中,您可以通过以下方式访问它:

connectionString = configuration.GetConnectionString("Local_One");

private readonly IConfiguration configuration;

public HomeController(IConfiguration config) 
{
    configuration = config;
}
connectionString = configuration.GetConnectionString("Local_One");