如何更改Asp.Net核心应用程序的端口号?

问题描述:

我在project.json中添加了以下部分。如何更改Asp.Net核心应用程序的端口号?

"commands": { 
    "run": "run server.urls=http://localhost:8082", 
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:8082", 
    "weblistener": "Microsoft.AspNet.Hosting --server WebListener --server.urls http://localhost:8082" 
    }, 

但是,它仍然显示 “现在侦听:http://localhost:5000” 在使用dotnet myapp.dll运行呢?

顺便说一句,从其他机器的客户端将能够访问该服务?

+0

的可能的复制[如何指定一个ASP.NET Core应用程序托管在端口?](http://*.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted- on) – Set

+0

''命令“'属性不再被'dotnet'使用,这就解释了为什么它不起作用。 – svick

是的,如果你绑定在任何外部IP地址上,这将从其他机器访问。例如绑定到http://*:80。请注意,绑定到http://localhost:80只会绑定在127.0.0.1接口上,因此不能从其他机器访问。

Visual Studio重写您的端口。您可以VS端口改变由代码编辑这个文件Properties\launchSettings.json否则设置:

 var host = new WebHostBuilder() 
      .UseKestrel() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseIISIntegration() 
      .UseStartup<Startup>() 
      .UseUrls("http://*:80") // <----- 
      .Build(); 

     host.Run(); 

使用外部配置文件一步一步的指南可here

,你也可以像这个代码

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

,并成立了由命令行应用程序:DOTNET运行--server.urls的http:// *:5555

+0

不错!但是这只能从命令行起作用。你知道如何/为什么从Visual Studio的端口被覆盖? –

+0

ConfigurationBuilder没有定义AddCommandLine。 窗帘向下。 – alerya

+0

@GerardoGrignoli如果从Visual Studio中启动您的应用程序,Properties/launchSettings.json文件将用于设置URL和端口。 –