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

ca9*_*3d9 27 .net-core asp.net-core

我在下面添加了以下部分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"
  },
Run Code Online (Sandbox Code Playgroud)

但是,它仍然显示"正在侦听:http:// localhost:5000 "运行时使用dotnet myapp.dll

顺便说一下,来自其他机器的客户能否访问该服务?

A.R*_*R.F 37

它对我有用。

我使用Asp.net core 2.2(这种方式在asp.net core 2.1更高版本中支持)。

Kestrelappsettings.json文件中添加部分。像这样:

{
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://localhost:4300"
      }
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)

并在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();
}
Run Code Online (Sandbox Code Playgroud)

  • 以及面向公众的“Url”:“http://*:80”(不用于生产,但对于向客户演示某些内容很有用) (2认同)

Ger*_*oli 34

是的,如果您绑定任何外部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();
Run Code Online (Sandbox Code Playgroud)

此处提供使用外部配置文件的分步指南.

  • 这本质上是我必须做的才能让我的应用程序在 Docker 中运行...尽管在这种情况下,我通过将环境变量 ASPNETCORE_URLS 设置为“http://*:80”,而不是“http://”来做到这一点。 /本地主机:80`。你让我很开心,谢谢! (2认同)

小智 14

其他机器上的客户端能够访问该服务吗?

添加到 appsettings.json

  "Urls": "http://0.0.0.0:8082",
Run Code Online (Sandbox Code Playgroud)


Adi*_*tYa 13

必须更改 3 个文件appsettings.json(请参阅最后一节 - kestrel ),launchsettings.json- applicationurl 注释掉,并更改 2 行Startup.cs

在文件中添加以下代码appsettings.json并根据需要将其移植到任何位置。

  },

 "Kestrel": {

   "Endpoints": {

     "Http": {

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

     }

   }

 }

 }
Run Code Online (Sandbox Code Playgroud)

用下面的行修改Startup.cs

using Microsoft.AspNetCore.Server.Kestrel.Core;

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


Gir*_*buC 12

在Asp.net core 2.0 WebApp中,如果您使用的是visual studio搜索LaunchSettings.json.我正在添加我的LaunchSettings.json,您可以更改端口号,因为您可以看到.

在此输入图像描述

  • 当我更新端口号并运行应用程序时,Girish Babu会向我抛出弹出式错误“无法连接到Web服务器IIS Express”,该如何解决? (2认同)

Tom*_*ang 12

简单地使用dotnet YouApp.dll --urls http://0.0.0.0:80

PS我不知道为什么我每次都需要谷歌这个,每次它都没有出现。所以在这里。


小智 9

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

在Properties-> LaunchSettings.json中。

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

发射

更改json文件中的端口号

在此处输入图片说明


小智 8

转到您的 program.cs 文件添加 UseUrs 方法来设置您的网址,确保您不使用保留的网址或端口

 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();
    }
Run Code Online (Sandbox Code Playgroud)


Len*_*rri 8

基于@Abdus Salam Azad 的回答......

例如,在 Visual Studio 2022 中,如果右键单击 ASP.NET Core Web API 项目,您可以访问此 UI,您可以在其中设置 ASPNETCORE 变量,如下所示:

在此输入图像描述

您可以在其中输入自定义 URL:端口,ASPNETCORE_URLS如下所示:

在此输入图像描述

https://本地主机:44777


Aks*_*ain 7

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

从而更改.UseStartup<Startup>()



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


Kol*_*n N 6

所有其他答案仅考虑httpURL。如果 URL 为https,则执行以下操作:

  1. launchsettings.json在 API 项目的属性下打开。

    在此输入图像描述

  2. 改变sslPort下面的iisSettings -> iisExpress

示例launchsettings.json如下所示

{
  "iisSettings": {
    "iisExpress": {
      "applicationUrl": "http://localhost:12345",
      "sslPort": 98765 <== Change_This
    }
  },
Run Code Online (Sandbox Code Playgroud)


Abd*_*zad 5

我们可以使用此命令通过 Windows Powershell 在单独的端口上运行我们的宿主项目,而无需 IIS 和 Visual Studio。krestel Web 服务器的默认值为 5001

$env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
Run Code Online (Sandbox Code Playgroud)


小智 5

以下适用于 ASP.Net Core 6.0。Program.cs 里面有这个:

var builder = WebApplication.CreateBuilder(args);

if (!app.Environment.IsDevelopment())
{
    builder.WebHost.UseUrls("http://*:80", "https://*.443");
}
Run Code Online (Sandbox Code Playgroud)

我发现在发布到生产环境时将其包装在条件语句中很有用,但这不是必需的。

这可以在 Mac OS 12 上的 Kestrel 服务器上运行。