【发布时间】:2017-09-01 10:46:31
【问题描述】:
当我使用 VS Code 进行本地开发时,我将使用 3000 端口,因为我是个潮人。非潮人希望它在服务器上的端口 8080 上。太好了,我们得到了这个。 Microsoft 文档给了我以下示例:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.Configure(app =>
{
app.Run(async (context) => await context.Response.WriteAsync("Hi!"));
})
.Build();
host.Run();
}
我不想使用hosting.json。我为什么要那个?对于这种情况,我有这个appsettings.{environment}.json 文件。亲爱的,我就把那个坏男孩粘贴进去
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddCommandLine(args)
.Build();
什么编译错误? env 在当前上下文中不存在。它只存在于Startup.cs 文件中——在启动时不会调用它,而是从启动文件Program.cs 中调用。
那么,我该如何解决这个问题呢?如何将特定于环境的托管设置存储在特定于环境的 appsettings.json 中,然后在通过 Program.cs 中的 WebHostBuilder 构建特定于环境的 Web 主机时使用它?
【问题讨论】:
-
你可以使用 launchSettings.json 来驱动它。
-
使用 vscode,但在任何一种情况下都希望有一个与 ide 无关的解决方案
标签: c# asp.net-core asp.net-core-mvc visual-studio-code asp.net-core-webapi