How to change the port number for Asp.Net core app

2020-02-03 09:03发布

I added the following section in 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"
  },

However, it still shows "Now listening on: http://localhost:5000" when run it using dotnet myapp.dll?

BTW, will clients from other machine be able to access the service?

9条回答
迷人小祖宗
2楼-- · 2020-02-03 09:35

We can use this command to run our host project via Windows Powershell without IIS and visual studio on a separate port. Default of krestel web server is 5001

$env:ASPNETCORE_URLS="http://localhost:22742" ; dotnet run
查看更多
再贱就再见
3楼-- · 2020-02-03 09:41

In Asp.net core 2.0 WebApp, if you are using visual studio search LaunchSettings.json. I am adding my LaunchSettings.json, you can change port no as u can see.

enter image description here

查看更多
\"骚年 ilove
4楼-- · 2020-02-03 09:43

In visual studio 2017 we can change the port number from LaunchSetting.json

In Properties-> LaunchSettings.json.

Open LaunchSettings.json and change the Port Number.

Launch

Change the port Number in json file

enter image description here

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-03 09:44

All the other answer accounts only for http URLs. If the URL is https, then do as follows,

  1. Open launchsettings.json under Properties of the API project.

    enter image description here

  2. Change the sslPort under iisSettings -> iisExpress

A sample launchsettings.json will look as follows

{
  "iisSettings": {
    "iisExpress": {
      "applicationUrl": "http://localhost:12345",
      "sslPort": 98765 <== Change_This
    }
  },
查看更多
祖国的老花朵
6楼-- · 2020-02-03 09:45

Use following one line of code .UseUrls("http://*:80") in Program.cs

Thus changing .UseStartup<Startup>()

to

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

查看更多
疯言疯语
7楼-- · 2020-02-03 09:51

It's working to me.

I use Asp.net core 2.2 (this way supported in asp.net core 2.1 and upper version).

add Kestrel section in appsettings.json file. like this:

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

and in 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();
}
查看更多
登录 后发表回答