cs返回错误的环境
本文关键字:环境 错误 返回 cs | 更新日期: 2023-09-27 18:12:10
Startup类包含
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();
}
但是environment . environmentname . tostring()返回"Production".
我已经在launchSettings.json中设置了ASPNETCORE_ENVIRONMENT为"Development"
当您在web.config
中设置环境时,通常也会发生这种情况。
例如,如果环境设置为launchSettings.json
中的Production
-
"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>
在这种情况下,当你试图读取startup.cs
env.EnvironmentName
时,你会得到Staging
这可能对某些人有帮助。
我在本地运行发布的文件-这不是通过visual studio F5运行的。通过命令行设置环境变量将其添加到不起作用的用户环境变量中。
我所要做的就是将ASPNETCORE_ENVIRONMENT添加到系统环境变量中。然后,重新启动后,它工作了。
获取环境变量:右键单击"这台电脑",然后单击属性。然后是高级系统设置。最后点击"环境变量"按钮。在下一个弹出窗口中添加系统变量
使用powershell命令:
$Env:ASPNETCORE_ENVIRONMENT = "Development"