How to set a default port for ASP.NET Core Angular app

后端 未结 4 1589
暖寄归人
暖寄归人 2021-01-01 18:45

I created a dotnet core project with the template ASP.NET Core with Angular using the dotnet CLI

dotnet new angular

Now, whenev

相关标签:
4条回答
  • 2021-01-01 18:55

    Okay, turns out, the changing port doesn't matter as I could access the Angular app from the url http://localhost:5000

    $ 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.
    

    I thought the port specified in this line is the one I should use to access the Angular app

    Starting @angular/cli on port 56082...
    

    turns out, it's not the one I should be using

    from MSDN:

    The app starts up an instance of the Angular CLI server in the background. A message similar to the following is logged: NG Live Development Server is listening on localhost:, open your browser on http://localhost:/. Ignore this message—it's not the URL for the combined ASP.NET Core and Angular CLI app.

    But when I was trying to access http://localhost:5000, I was getting an SSL error, upon more digging, I found this line in Startup.cs

    app.UseHttpsRedirection();
    

    After adding this condition, it's working fine now and I'm able to access the Angular app at http://localhost:5000

    if (!env.IsDevelopment())
        app.UseHttpsRedirection();
    
    0 讨论(0)
  • 2021-01-01 19:10

    Try the following command:

    ng set defaults.serve.port=4201

    0 讨论(0)
  • 2021-01-01 19:16

    Generally speaking, you are correct, however I've seen a few other locations where this value might be overridden. One would be in the package.json, specifically in this block:

      "scripts": {
        "ng": "ng",
        "start": "ng serve --sourcemap --extractCss --host 0.0.0.0 --proxy-config proxy.config.json",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      }
    

    Also you might check in a file called proxy.config.json if you have it

    0 讨论(0)
  • 2021-01-01 19:18

    The SpaServices are taking the "ng serve" command from the package.json file and adding a random port before running the command. In Windows, to get around this add your own port command and the suffix "&REM" which tells the command processor that anything after that is a comment.

    "scripts": {
      "ng": "ng",
      "start": "ng serve --hmr --o --port=5002 &REM",
      ...
      }
    

    In the command window you will see something similar to the following when it's processed:

      > ng serve --hmr --o --port=5002 &REM "--port" "58198"
    
    0 讨论(0)
提交回复
热议问题