3

啓動類包含Startup.cs返回錯誤的環境

public Startup(IHostingEnvironment env) 
{ 
    var builder = new ConfigurationBuilder() 
     .SetBasePath(env.ContentRootPath) 
     .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
     .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

    Console.WriteLine($"{env.EnvironmentName.ToString()}"); 

    if (env.IsDevelopment()) 
    { 
     // For more details on using the user secret store see 
     // https://go.microsoft.com/fwlink/?LinkID=532709 
     builder.AddUserSecrets(); 
    } 

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

但env.EnvironmentName.ToString()返回 「生產」。

我已經設置我ASPNETCORE_ENVIRONMENT到「發展」在launchSettings.json

+3

如何啓動這個項目?項目設置/ launchSettings.json中的「ASPNETCORE_ENVIRONMENT」僅適用於通過F5/Ctrl + F5從Visual Studio開始。通過dotnet或任何其他方式啓動它不適用。 launchSattings.json是一個純粹的VIsual Studio Mechanic – Tseng

+0

無論如何設置環境變量。我使用visual studio代碼和dotnet核心CLI進行開發。 – datkom

+0

如果你像命令那樣運行它,使用你的環境shell命令來設置環境變量(CommandLine,Powershell,bash或者MacOS使用的其他任何東西) – Tseng

回答

0

得到這個工作使用PowerShell命令:

$Env:ASPNETCORE_ENVIRONMENT = "Development"

0

,當你在web.config已經安裝環境太這通常發生。

例如,如果您有環境設定ProductionlaunchSettings.json -

"profiles": { 
    "IIS Express": { 
     "commandName": "IISExpress", 
     "launchBrowser": true, 
     "environmentVariables": { 
     "ASPNETCORE_ENVIRONMENT": "Production" 
     } 
    }, 

而且在web.config,如果你有其他環境Staging -

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"> 
    <environmentVariables> 
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" /> 
    </environmentVariables> 
</aspNetCore> 

在這種情況下,你會得到Staging當您正在嘗試閱讀env.EnvironmentNamestartup.cs

看看這是否有幫助。

+0

嘗試過把''放到web.config中,但仍然不起作用。我使用visual studio代碼和dotnet核心CLI進行開發。 – datkom

+0

如果你不使用VS2015,那麼你可以像命令提示那樣設置變量 - 「set ASPNETCORE_ENVIRONMENT = Development」,然後運行'dotnet'命令。 – Sanket

+0

已將其設置爲「設置ASPNETCORE_ENVIRONMENT =開發」。仍然得到相同的結果 – datkom