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

后端 未结 12 994
误落风尘
误落风尘 2020-12-05 01:34

I added the following section in project.json.

  \"commands\": {
    \"run\": \"run server.urls=http://localhost:8082\",
    \"web\": \"Microsof         


        
12条回答
  •  Happy的楠姐
    2020-12-05 02:28

    If you want to run on a specific port 60535 while developing locally but want to run app on port 80 in stage/prod environment servers, this does it.

    Add to environmentVariables section in launchSettings.json

    "ASPNETCORE_DEVELOPER_OVERRIDES": "Developer-Overrides",
    

    and then modify Program.cs to

    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()
                        .UseNLog();                   
                    });
    

提交回复
热议问题