在没有 Visual Studio 2015 的情况下配置 .NET Core Web 应用程序启动设置

Configuring .NET Core web application launch settings without Visual Studio 2015

我正在关注 .NET Core RC2 文档,大部分信息是在 Visual Studio 2015 年编写的。但是,他们也发布了 Visual Studio 代码,我也可以 运行 应用程序从命令行。

当我在没有 VS2015 的情况下开发应用程序时,我无法

  1. 连接 launchSettings.json 到我的应用程序
  2. 更改启动设置,例如Hosting EnvironmentlaunchUrl 或默认 port 5000

如果我在没有 Visual Studio 的情况下进行开发,我该如何设置这些属性?

完全可以在没有 Visual Studio 的情况下构建 .NET Core 应用程序。 Visual Studio 代码或 dotnet CLI 可以处理您需要做的一切,只是有点不同。

据我所知,launchSettings.json 是 Visual Studio 特有的。来自 documentation:

This file holds settings specific to each profile Visual Studio is configured to use to launch the application, including any environment variables that should be used.

您需要一种不同的方式来设置所需的选项:

绑定地址和端口

这可以在您 bootstrap 您在 Program.cs 中的应用程序时配置:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseUrls("http://localhost:8080") // name and port to listen on
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

托管环境

来自同一个docs

ASP.NET Core references a particular environment variable, ASPNETCORE_ENVIRONMENT to describe the environment the application is currently running in. This variable can be set to any value you like, but three values are used by convention: Development, Staging, and Production.

只需将适合您平台的环境变量设置为 "Development"、"Staging"、"Production" 等。ASP.NET 核心将读取值和所有 IHostingEnvironment 逻辑将起作用。

在Windows中,可以在GUI或者命令行中设置环境变量:

setx ASPNETCORE_ENVIRONMENT "Development"

启动URL

Visual Studio 试图通过在您 运行 您的应用程序时自动打开浏览器来提供帮助。如果您使用 dotnet,则必须手动执行此操作。

我还没有尝试过,但是 Visual Studio 代码的这个插件可能会为您提供该功能:Visual Studio Chrome Debugger