ASP.NET Core設定port

  • 1045
  • 0

ASP.NET Core透過launchsettings.json設定port

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54339/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "Kestrel Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:51997/"
    },
    "EnvironmentsSample": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    }
  }
}

根據官方文件設定檔來看,

commandName可被設定為以下3種:

  • IISExpress
  • IIS
  • Project (which launches Kestrel)

當執行dotnet run指令時會執行第一個 "commandName": "Project",

因此以目前設定會啟動 http://localhost:51997/

上圖第2行可以看到 Hosting environment被設定為 Staging

是因為launchSettings.json中設定的 ASPNETCORE_ENVIRONMENT

ASPNETCORE_ENVIRONMENT在Configure中則是以IHostingEnvironment的型別傳入藉以判斷要執行的環境

 

ref:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2