简体   繁体   English

如何在 VS Code 中为 ASP.NET Core 设置默认端口?

[英]How to set default port for ASP.NET Core in VS Code?

I develop an Angular app based on ASP.NET Core and there is some settings in launchSettings.json in order to run the app with the given ports as shown below:我开发了一个基于 ASP.NET Core 的 Angular 应用程序,并且在launchSettings.json中有一些设置,以便使用给定的端口运行应用程序,如下所示:

"profiles": {
  "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
      "DOTNET_ENVIRONMENT": "Development"
    }
  },
  "EmployeeProject.WebUI": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:6000;https://localhost:6001",
      "environmentVariables": {
      "DOTNET_ENVIRONMENT": "Development"
    }
  }
}

However, I heard that these settings is ignored when using VS Code and when running the frontend and backend at the same time using dotnet watch run the app starts by using random ports in every run.但是,我听说在使用VS Code时会忽略这些设置,并且在使用dotnet watch run同时运行前端和后端时,应用程序在每次运行时都使用随机端口启动。 So, how can I make the app starts using the same ports (6000 or 6001) via dotnet run or dotnet watch run in VS Code?那么,如何通过 VS Code 中的dotnet rundotnet watch run使应用程序开始使用相同的端口(6000 或 6001)?

If you want to do this the VS Code way, as long as you use F5 (or the Run > "Start Debugging" command), it's as simple as changing the launch.json file from this:如果您想以 VS Code 的方式执行此操作,只要您使用 F5(或“运行”>“开始调试”命令),就像更改launch.json文件一样简单:

...
"env": {
    "ASPNETCORE_ENVIRONMENT": "Development"
},
...

to this:对此:

...
"env": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_URLS": "http://localhost:5001"
},
...

Otherwise, if you use the IIS Express profile, edit your launchSettings.json file to specify the port:否则,如果您使用 IIS Express 配置文件,请编辑您的launchSettings.json文件以指定端口:

"iisSettings": {
  "iisExpress": {
    "applicationUrl": "http://localhost:6000",
  }
},
"profiles" : { ... }

Running from command line execute Kastrel server not IIS .从命令行运行执行Kastrel服务器而不是IIS In that case probably configuration appsettings.json is use.在这种情况下,可能会使用配置appsettings.json You can put in this configuration section to control port:您可以在此配置部分中放入控制端口:

    "Kestrel": {
        "Endpoints": {
            "HTTP": {
                "Url": "http://localhost:6000"
            }
        }
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM