Dotnet不了解特定于环境的appsettings文件

时间:2017-06-04 08:00:37

标签: asp.net-core

首先,这个问题与dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json的问题不同,我确信环境名称没有问题。

  

os:linux(ubuntu 14.04)

     

dotnet --version:1.0.3

Shell脚本:

  

导出ASPNETCORE_ENVIRONMENT = Cloud9

     

cd ../../src/Project

     

dotnet restore

     

dotnet publish -c Debug -o bin / Debug / out /

     

cp ../../.config/Project /appsettings.Cloud9.json bin / Debug / out / #copy设置文件到发布目录

     

dotnet bin / Debug / out / Project.dll --urls" http://0.0.0.0:8080"

当我在上面的脚本上运行时,它成功地为网站提供服务,但appsettings.json值而不是appsettings.Cloud9.json

以下是我的Startup.cs构造函数和Program.cs

    //Sturtup.cs
    public IConfigurationRoot Configuration { get; }
    public Startup(ILoggerFactory loggerFactory, IHostingEnvironment environment)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true);

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    //Program.cs
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .Build();
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseConfiguration(config)
            .Build();

        host.Run();
    }

注意:如果我在没有发布的情况下执行dotnet run --urls ...,它将按预期运行。

第二个注释:我在out目录中看到appsettings.Cloud9.json

1 个答案:

答案 0 :(得分:1)

添加cd ./bin/Debug/out后解决。所以正确的脚本是:

export ASPNETCORE_ENVIRONMENT=Cloud9
cd ../../src/Project
dotnet restore
dotnet publish -c Debug -o bin/Debug/out/  
cp ../../.config/Project /appsettings.Cloud9.json bin/Debug/out/

关键点:

cd ./bin/Debug/out
dotnet Project.dll --urls "http://0.0.0.0:8080"