引用appsettings.json的连接字符串的单独文件?

问题描述 投票:1回答:1

在我的.net框架项目中,我希望在app.config中有以下内容

 <connectionStrings configSource="connections.config" />

与connections.config包含类似的东西

 <connectionStrings>

   <add name="ApplicationDatabase" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=mydatabase;User Id=myuser; PWD=mypassword;"  />

</connectionStrings>

然后我不检查connections.config到源代码。

我想在.NET Core中使用类似的技巧,但它使用了appsettings.json

通过

     services.AddDbContext<ApiDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("MyDatabase")));

我该怎么办?

json asp.net-core
1个回答
2
投票

您可以创建单独的JSON文件(或XML,或任何配置提供程序)并在应用程序启动时加载它:

WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration(builder =>
    {
        builder.AddJsonFile("connectionstrings.json", optional: true);
    })

查看所有支持的配置提供程序的the docs

© www.soinside.com 2019 - 2024. All rights reserved.