.NET Core project.json 命令 - 设置 ASPNETCORE_ENVIRONMENT
.NET Core project.json commands - Set ASPNETCORE_ENVIRONMENT
从命令行,如果我 运行 set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run
,我的托管环境设置为开发。
但是,如果我将同一行作为命令添加到我的 project.json 文件中,观察程序的托管环境始终是生产环境:
"commands": {
"watch": "set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run"
},
是否有任何参数可以传递给 dotnet 运行 来开发托管环境?我确实需要它作为命令工作。
您可以添加从命令行读取配置的 Microsoft.Extensions.Configuration.CommandLine
包:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
使用 dotnet run
,您可以执行以下操作:
dotnet run --ASPNETCORE_ENVIRONMENT Development
你应该也可以用 dotnet watch run
做类似的事情。
我终于明白了!
诀窍是右键单击项目,转到属性,然后 select 调试选项卡。接下来,在配置文件下,我 select 编辑了我 projects.json 中定义的命令的名称:"watch"。 selected 后,我单击“通过环境变量添加”,并添加了 Name/Value 对 ASPNETCORE_ENVIRONMENT 和开发。
这实际上是在解决方案资源管理器的属性下上传 launchSettings.json 文件。该文件也可以手动编辑。输出看起来像这样:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56846/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"LendingTree.CreditCards.Web": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Watcher Development": {
"commandName": "Watcher Development",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
从命令行,如果我 运行 set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run
,我的托管环境设置为开发。
但是,如果我将同一行作为命令添加到我的 project.json 文件中,观察程序的托管环境始终是生产环境:
"commands": {
"watch": "set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run"
},
是否有任何参数可以传递给 dotnet 运行 来开发托管环境?我确实需要它作为命令工作。
您可以添加从命令行读取配置的 Microsoft.Extensions.Configuration.CommandLine
包:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
使用 dotnet run
,您可以执行以下操作:
dotnet run --ASPNETCORE_ENVIRONMENT Development
你应该也可以用 dotnet watch run
做类似的事情。
我终于明白了!
诀窍是右键单击项目,转到属性,然后 select 调试选项卡。接下来,在配置文件下,我 select 编辑了我 projects.json 中定义的命令的名称:"watch"。 selected 后,我单击“通过环境变量添加”,并添加了 Name/Value 对 ASPNETCORE_ENVIRONMENT 和开发。
这实际上是在解决方案资源管理器的属性下上传 launchSettings.json 文件。该文件也可以手动编辑。输出看起来像这样:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56846/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"LendingTree.CreditCards.Web": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Watcher Development": {
"commandName": "Watcher Development",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}