【问题标题】:How to change the port number for Asp.Net core app?如何更改 Asp.Net 核心应用程序的端口号?
【发布时间】:2016-12-09 21:05:39
【问题描述】:

我在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”?

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

【问题讨论】:

标签: c# asp.net-core .net-core


【解决方案1】:

是的,如果您绑定到任何外部 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://localhost:80") // <-----
            .Build();

        host.Run();

here 提供了使用外部配置文件的分步指南。

【讨论】:

  • 另外,可以在Overriding configuration查看微软关于覆盖配置的参考
  • 这条语句帮助了我:您可以通过编辑更改 VS 端口 Properties\launchSettings.json 您可以定义新的启动配置文件,添加防火墙例外,然后让前端开发人员使用当你们两个一起进行更改时,您的实时服务器。
【解决方案2】:

它对我有用。

我使用Asp.net core 2.2asp.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();
}

【讨论】:

  • "Url": "http://*:80" 面向公众(不要用于生产,但对例如向客户演示某些东西很有用)
  • 错误 CS0104 'IHostingEnvironment' 是 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' 和 'Microsoft.Extensions.Hosting.IHostingEnvironment' 之间的模糊引用
  • @MindRoasterMir; IHostingEnvironment 引用自 Microsoft.AspNetCore.Hosting
【解决方案3】:

在 Asp.net core 2.0 WebApp 中,如果您使用 Visual Studio 搜索 LaunchSettings.json。我正在添加我的 LaunchSettings.json,您可以更改端口号,如您所见。

【讨论】:

  • Girish Babu 当我更新端口号并运行应用程序时,它会在弹出窗口中提示“无法连接到 Web 服务器“IIS Express”,如何解决这个问题?
  • @RohitSharma,您计算机上的另一个进程可能正在使用更新的端口号。试试其他号码。
【解决方案4】:

只需使用dotnet YouApp.dll --urls http://0.0.0.0:80

附:我不知道为什么我每次都需要用谷歌搜索它,而且每次它都没有出现。就是这样。

【讨论】:

  • 你并不孤单。因为所有开发者都有一个问题:如何更改 WebAPI 端口但微软说:添加 urls 参数
【解决方案5】:

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

在属性-> LaunchSettings.json.

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

更改json文件中的端口号

【讨论】:

    【解决方案6】:

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

    从而将.UseStartup&lt;Startup&gt;()

    更改为

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

    【讨论】:

      【解决方案7】:

      转到您的 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();
          }
      

      【讨论】:

        【解决方案8】:

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

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

          },
        
         "Kestrel": {
        
           "Endpoints": {
        
             "Http": {
        
               "Url": "http://localhost:5003"
        
             }
        
           }
        
         }
        
         }
        

        用下面几行修改Startup.cs

        using Microsoft.AspNetCore.Server.Kestrel.Core;

        services.Configure&lt;KestrelServerOptions&gt;(Configuration.GetSection("Kestrel"));

        【讨论】:

          【解决方案9】:

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

          $env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
          

          【讨论】:

            【解决方案10】:

            所有其他答案仅用于http URL。如果网址为https,则执行如下操作,

            1. 在 API 项目的 Properties 下打开 launchsettings.json

            2. 更改iisSettings -&gt; iisExpress下的sslPort

            launchsettings.json 示例如下所示

            {
              "iisSettings": {
                "iisExpress": {
                  "applicationUrl": "http://localhost:12345",
                  "sslPort": 98765 <== Change_This
                }
              },
            

            【讨论】:

              【解决方案11】:

              其他机器的客户端可以访问服务吗?

              添加到 appsettings.json

                "Urls": "http://0.0.0.0:8082",
              

              【讨论】:

              • 此调整在 .NET 5 中的 ASP.NET Core 中效果很好
              • 对我不起作用(ASP.NET Core 5)
              【解决方案12】:

              你也可以这样写

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

              并通过命令行设置您的应用程序:dotnet run --server.urls http://*:5555

              【讨论】:

              • 不错!但这仅适用于命令行。你知道如何/为什么从 Visual Studio 中覆盖端口吗?
              • ConfigurationBuilder 没有定义 AddCommandLine。拉下窗帘。
              • @GerardoGrignoli 如果从 Visual Studio 中启动您的应用程序,Properties/launchSettings.json 文件将用于设置 url 和端口。
              【解决方案13】:

              在您的 hosts.json 中 将"Url": "http://localhost:80" 替换为"Url": "http://*:80",您现在可以通过http://your_local_machine_ip:80 访问您的应用程序,例如http://192.168.1.4:80

              【讨论】:

                【解决方案14】:

                也许是因为我还没有使用 Core。我的项目没有 LaunchSettings.json 文件,但这确实提示我查看项目属性。我在 Web 选项卡下找到它并简单地更改了项目 url:

                【讨论】:

                  【解决方案15】:

                  在 appsetting.json 中

                  { “调试模式”:假, “网址”:“http://localhost:8082” }

                  【讨论】:

                    【解决方案16】:

                    升级到 .NET 6 后,我在 Kubernetes 部署中遇到了类似问题。解决方案只是将以下环境变量添加到部署中:

                    - name: Kestrel__Endpoints__Http__Url
                      value: http://0.0.0.0:80
                    

                    这将适用于您可以使用环境变量的其他任何地方

                    【讨论】:

                      【解决方案17】:

                      如果您想在本地开发时在特定端口 60535 上运行,但又想在 stage/prod 环境服务器中的端口 80 上运行应用程序,可以这样做。

                      添加到 launchSettings.json 中的 environmentVariables 部分

                      "ASPNETCORE_DEVELOPER_OVERRIDES": "Developer-Overrides",
                      

                      然后修改Program.cs为

                      public static IHostBuilder CreateHostBuilder(string[] args) =>
                                  Host.CreateDefaultBuilder(args)
                                      .ConfigureWebHostDefaults(webBuilder =>
                                      {
                                          webBuilder.UseKestrel(options =>
                                          {
                                              var devOverride = Environment.GetEnvironmentVariable("ASPNETCORE_DEVELOPER_OVERRIDES");
                                              if (!string.IsNullOrWhiteSpace(devOverride))
                                              {
                                                  options.ListenLocalhost(60535);
                                              }
                                              else
                                              {
                                                  options.ListenAnyIP(80);
                                              }
                                          })
                                          .UseStartup<Startup>()
                                          .UseNLog();                   
                                      });
                      

                      【讨论】:

                        【解决方案18】:

                        我使用 Visual Studio 2022 创建了我的项目,因此在 Project/Properties/launchSettings.json 中有两个部分用于此主题: 1- 在 IISExpress 中享用午餐:

                        "iisSettings": {
                        ..
                        "iisExpress": {
                          "applicationUrl": "http://localhost:31520",
                          "sslPort": 44346
                        }
                        

                        },

                        2- 通过 IDE 午餐:

                        "profiles": {
                        "MaxTemplate.API": {
                          ...
                          "applicationUrl": "https://localhost:7141;http://localhost:5141",
                          ...
                          }
                        },
                        

                        例如,您可以将端口 7141 更改为 5050 并再次运行项目。

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2020-08-07
                          • 2018-03-25
                          • 2022-08-23
                          • 2018-10-09
                          • 2022-12-09
                          • 1970-01-01
                          • 2018-09-13
                          相关资源
                          最近更新 更多