如何修改Asp.Net core应用程序的端口号?

119

我在project.json中添加了以下部分。

  "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"
  },

不过,当使用 dotnet myapp.dll 运行时它仍然显示 "现在侦听于: http://localhost:5000"?

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


"commands" 属性不再被 dotnet 使用,这就解释了为什么它不起作用。 - svick
在appsettings.json中配置端口 - menxin
24个回答

78

如果您绑定到任何外部IP地址,那么这将可以从其他计算机访问。例如,绑定到http://*:80。请注意,绑定到http://localhost:80仅会绑定到127.0.0.1接口,因此无法从其他计算机访问。

Visual Studio正在覆盖您的端口。您可以通过编辑此文件Properties\launchSettings.json来更改VS端口,或者通过代码进行设置:

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

        host.Run();

有关如何使用外部配置文件的逐步指南可以在此处找到。


2
这基本上就是我必须做的,以使我的应用程序在Docker中运行...尽管在那种情况下,我通过将环境变量ASPNETCORE_URLS设置为http:// *:80而不是http://localhost:80来实现。你让我的一天,谢谢! - JamesQMurphy
当我的代码使用WebApplication.CreateBuilder(args)时,我应该使用什么? - undefined

71

对我来说它有效。

我使用 Asp.net core 2.2(这种方式支持 asp.net core 2.1 及以上版本)。

appsettings.json 文件中添加 Kestrel 部分,就像这样:

{
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://localhost:4300"
      }
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

并且在 Startup.cs 中:

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
      var builder = new ConfigurationBuilder()
         .SetBasePath(env.ContentRootPath)
         .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
         .AddEnvironmentVariables();

      Configuration = builder.Build();
}

2
而且,“Url”:“http://*:80”用于公共界面(不要用于生产,但例如向客户演示某些内容非常有用)。 - Ruslan

63

使用简单的 dotnet YouApp.dll --urls http://0.0.0.0:80 命令。

P.S. 我不知道为什么每次都需要谷歌,而且每次都找不到。所以在这里提供一下。


对我没有用,使用的是dotnet 6。项目仍然尝试在默认端口上运行(在我的情况下是5000)。 - Sheldonfrith
1
@Sheldonfrith 没有意义。我在ASP.Net Core 3.1和ASP.Net Core 6上使用相同的命令,它们都可以工作。 在ASP.Net Core 7文档中,明确指出了一种配置端口的方法:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-7.0 您可以检查是否已配置了ASPNETCORE_URLS,这可能会导致冲突。 - Tom Charles Zhang

31
在Asp.net core 2.0 WebApp中,如果您使用Visual Studio,请搜索 LaunchSettings.json 。我正在添加我的LaunchSettings.json文件,您可以像下面那样更改端口号。

输入图片描述


18

在 Visual Studio 2017 中,我们可以从 LaunchSetting.json 更改端口号。

在属性 -> LaunchSettings.json 中。

打开 LaunchSettings.json 并更改端口号。

Launch

修改json文件中的端口号

enter image description here


16

Program.cs 中使用以下一行代码:.UseUrls("http://*:80")

这将更改原来的 .UseStartup<Startup>()

.UseStartup<Startup>() .UseUrls("http://*:80")


13
"其他机器的客户端能够访问该服务吗?"
请在appsettings.json中添加。
  "Urls": "http://0.0.0.0:8082",

12

需要更改3个文件:appsettings.json(见最后一节 - kestrel),launchsettings.json中的应用程序URL已注释掉,以及Startup.cs中的2行代码更改。

请将下面的代码添加到appsettings.json文件中,并根据需要将端口设置为任何值。

  },

 "Kestrel": {

   "Endpoints": {

     "Http": {

       "Url": "http://localhost:5003"

     }

   }

 }

 }

使用以下代码修改 Startup.cs 文件。

using Microsoft.AspNetCore.Server.Kestrel.Core;

services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));


10
我们可以使用这个命令在Windows Powershell上运行我们的主机项目,而不需要IIS和Visual Studio,并将其运行在一个独立的端口。默认情况下,krestel Web服务器的端口是5001。
$env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run

9

打开 program.cs 文件,添加 UseUrs 方法以设置您的 URL,确保不要使用保留的 URL 或端口。

 public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()

                // params string[] urls
                .UseUrls(urls: "http://localhost:10000")

                .Build();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接