无法在launchSettings.JSON中为Net.Core应用程序设置端口

Kon*_*ten 4 json asp.net-core

我编辑了launchSettings.JSON文件并更改了端口.

"Gdb.Blopp": {
  "commandName": "Project",
  "launchBrowser": false,
  "launchUrl": "http://localhost:4000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
Run Code Online (Sandbox Code Playgroud)

不过,它仍然从5000端口开始.那个设置是否被忽视了,还是我错过了别的什么?

Tse*_*eng 7

launchSettings.json当您点击F5/Ctr + F5并从开始按钮旁边的下拉菜单中提供选项时,应该由IDE(即Visual Studio)使用.

在此输入图像描述

此外,您不应该直接编辑该launcherSettings.json文件,而是使用项目属性来更改内容.

其中一个原因是,如果通过项目属性更改它,Visual Studio还将编辑IIS Express文件(位于.vs/config/applicationhost.config解决方案的文件夹中).

如果您想更改使用的kestrel端口,请使用.UseUrls("http://0.0.0.0:4000")(从appsettings.json或获取hosting.json)Program.cs.

如果你不想使用硬编码,你也可以这样做

创建一个hosting.json:

{
  "server": "Microsoft.AspNetCore.Server.Kestrel",
  "server.urls": "http://localhost:4000"
}
Run Code Online (Sandbox Code Playgroud)

Program.cs中

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("hosting.json", optional: false)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
Run Code Online (Sandbox Code Playgroud)

您也可以通过命令行执行此操作(AddCommandLine此处调用很重要,来自Microsoft.Extensions.Configuration.CommandLine"包).

var config = new ConfigurationBuilder()
    .AddCommandLine(args)
    .Build();

var host = new WebHostBuilder()
    .UseConfiguration(config)
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

host.Run();
Run Code Online (Sandbox Code Playgroud)

然后通过它运行dotnet run server.urls=http://0.0.0.0:4000.

当您运行IIS/IISExpress时,将确定红场端口UseIISIntegration().