如何为ASP.NET Core Angular应用程序设置默认端口

cyb*_*e92 9 asp.net-core angular

我使用dotnet CLI使用带有Angular的ASP.NET Core模板创建了一个dotnet核心项目

dotnet new angular

现在,每当我使用dotnet run命令运行应用程序时,angular每次都使用不同的端口.

我尝试在angular-cli.json中设置默认的服务端口

"defaults": {
  ..
  "serve": {
    "port": 4200
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然不起作用.

我在网上找不到任何文档,那么在哪里可以设置默认端口?

Ste*_*veC 12

SpaServices正在从package.json文件中获取“ ng serve”命令,并在运行该命令之前添加一个随机端口。在Windows中,要解决此问题,请添加您自己的port命令和后缀“&REM”,该后缀告诉命令处理器此后的所有内容均为注释。

"scripts": {
  "ng": "ng",
  "start": "ng serve --hmr --o --port=5002 &REM",
  ...
  }
Run Code Online (Sandbox Code Playgroud)

处理后,在命令窗口中,您将看到类似于以下内容:

  > ng serve --hmr --o --port=5002 &REM "--port" "58198"
Run Code Online (Sandbox Code Playgroud)


cyb*_*e92 5

好的,事实证明,更改端口并不重要,因为我可以从URL http:// localhost:5000访问Angular应用程序

$ dotnet run
Using launch settings from /Users/raviteja/Desktop/CSharp/SignalR-Angular-ChatApp/Properties/launchSettings.json...
: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/Users/raviteja/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.AspNetCore.SpaServices[0]
      Starting @angular/cli on port 56082...
Hosting environment: Development
Content root path: /Users/raviteja/Desktop/CSharp/SignalR-Angular-ChatApp
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.SpaServices[0]
      > SignalR_Angular_ChatApp@0.0.0 start /Users/raviteja/Desktop/CSharp/SignalR-Angular-ChatApp/ClientApp
      > ng serve --extract-css "--port" "56082"

      ** NG Live Development Server is listening on localhost:56082, open your browser on http://localhost:56082/ **

info: Microsoft.AspNetCore.SpaServices[0]
      Date: 2018-05-31T23:44:11.061Z

info: Microsoft.AspNetCore.SpaServices[0]
      Hash: 7f8c3d48fb430eed2337
      Time: 13815ms
      chunk {inline} inline.bundle.js (inline) 3.85 kB [entry] [rendered]
      chunk {main} main.bundle.js (main) 50.5 kB [initial] [rendered]
      chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial] [rendered]
      chunk {styles} styles.bundle.css (styles) 119 kB [initial] [rendered]
      chunk {vendor} vendor.bundle.js (vendor) 9.5 MB [initial] [rendered]


info: Microsoft.AspNetCore.SpaServices[0]
      webpack: Compiled successfully.
Run Code Online (Sandbox Code Playgroud)

我以为这一行中指定的端口是我应该用来访问Angular应用程序的端口

Starting @angular/cli on port 56082...
Run Code Online (Sandbox Code Playgroud)

事实证明,这不是我应该使用的那个

MSDN

该应用程序在后台启动Angular CLI服务器的实例。记录类似于以下消息:NG Live Development Server正在localhost:上侦听,请在http:// localhost:/ 上打开浏览器。忽略此消息 -它不是 组合的ASP.NET Core和Angular CLI应用程序的URL。

但是,当我尝试访问http:// localhost:5000时,出现了SSL错误,经过进一步挖掘,我在Startup.cs中找到了这一行。

app.UseHttpsRedirection();
Run Code Online (Sandbox Code Playgroud)

添加此条件后,它现在可以正常工作,并且我能够通过http:// localhost:5000访问Angular应用程序

if (!env.IsDevelopment())
    app.UseHttpsRedirection();
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果你使用 5000 端口并且你_touch_一个 Angular 文件,它会重新编译,但浏览器不会自动重新加载。 (2认同)