【问题标题】:Can't set the port for Net.Core application in launchSettings.JSON无法在 launchSettings.JSON 中设置 Net.Core 应用程序的端口
【发布时间】:2017-03-25 20:24:38
【问题描述】:

我已经编辑了 launchSettings.JSON 文件并像这样更改了端口。

"Gdb.Blopp": {
  "commandName": "Project",
  "launchBrowser": false,
  "launchUrl": "http://localhost:4000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }

不过,它仍然从端口 5000 开始。是完全忽略了该设置还是我错过了其他内容?

【问题讨论】:

  • launchSettings.json 仅适用于 VIsual Studio,当您按下 F5 时。但是您应该直接编辑该文件,而不是使用项目属性来更改内容。因为如果您更改项目属性,它还将编辑位于 .vs/config/applicationhost.config 中的 IIS Express 文件,如果您想更改 kestrel 使用的端口,请在运行 IIS/IISExpress 时使用 .UseUrls("http://0.0.0.0:4000") in Program.cs,kestrel 端口将由UseIISIntegration()确定
  • @Tseng Nice,这就是我开始怀疑 googlearching 的最后几分钟。从可编译代码而不是 JSON 设置文件设置端口似乎很奇怪。无论如何,请发表您的评论作为回复,以便我接受。
  • 您也可以通过配置文件或命令行参数进行设置,请参阅下面的答案。刚刚超出了评论的范围。对于命令

标签: json asp.net-core


【解决方案1】:

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

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

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

如果您想更改红隼使用的端口,请在Program.cs 中使用.UseUrls("http://0.0.0.0:4000")(从appsettings.jsonhosting.json 获取)。

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

创建hosting.json

{
  "server": "Microsoft.AspNetCore.Server.Kestrel",
  "server.urls": "http://localhost:4000"
}

程序.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();
    }
}

您也可以通过命令行执行此操作(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();

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

当您运行 IIS/IISExpress 时,红隼端口将由 UseIISIntegration() 确定。

【讨论】:

  • 您的意思是应该还是不应该手动更改launcherSettings.json文件?我怀疑您的意思是 not 来更改它,并且这是您的答案中的错字。请指教。
  • @KonradViltersten:是的,对不起。修复它
  • 只是为了帮助他人,最好添加所需的using,在我的情况下,我必须包含using Microsoft.Extensions.Configuration; 才能使ConfigurationBuilder 工作。在 VS 中这很容易,它只会告诉你你错过了它,但在 VSCode 中我没有看到提供的帮助(除非我的 C# 插件配置不正确)
  • 我无法通过命令行运行它:dotnet run server.urls=0.0.0.0:4000。不得不写:dotnet run --urls=0.0.0.0:4000
【解决方案2】:

从 .NET Core 2.0 开始,您无需再维护 hosting.json 或修改应用启动。内置支持设置应用程序端口,此处解释:https://stackoverflow.com/a/49000939/606007

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 2021-01-13
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    相关资源
    最近更新 更多