相关疑难解决方法(0)

在asp.net核心中自动为dev和release环境设置appsettings.json?

我已经在我appsettings.json的数据库连接字符串,webapi位置等内容中定义了一些值,这些值对于开发,登台和实时环境是不同的.

有没有办法有多个appsettings.json文件(如appsettings.live.json,等等),并让asp.net应用程序'知道'根据它运行的构建配置使用哪一个?

c# appsettings asp.net-core

54
推荐指数
11
解决办法
7万
查看次数

尽管需要使用using语句和扩展,但无法调用函数

所以我希望能够dotnet在终端上运行(.net core mvc-project)时选择我的环境.我找到了这篇文章,并认为第二个最高答案是一个简洁的解决方法,简而言之:

用以下内容替换Program类主体:

private static readonly Dictionary<string, string> defaults =
new Dictionary<string, string> {
    { WebHostDefaults.EnvironmentKey, "development" }
};

public static void Main(string[] args)
{
    var configuration =
        new ConfigurationBuilder()
            .AddInMemoryCollection(defaults)
            .AddEnvironmentVariables("ASPNETCORE_")
            .AddCommandLine(args)
            .Build();

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}
Run Code Online (Sandbox Code Playgroud)

然后运行:

dotnet run environment=development
dotnet run environment=staging
Run Code Online (Sandbox Code Playgroud)

所以我粘贴了它,它说我需要添加一个使用的声明

using Microsoft.Extensions.Configuration;

仍然收到此错误消息:

'IConfigurationBuilder' does not contain a definition for
'AddCommandLine' and no extension method 'AddCommandLine' 
accepting a first argument of …
Run Code Online (Sandbox Code Playgroud)

c# asp.net .net-core

3
推荐指数
1
解决办法
144
查看次数

标签 统计

c# ×2

.net-core ×1

appsettings ×1

asp.net ×1

asp.net-core ×1