如何为ASP.NET Core Angular应用设置默认端口 [英] How to set a default port for ASP.NET Core Angular app

查看:154
本文介绍了如何为ASP.NET Core Angular应用设置默认端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

dotnet new angular

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

Now, whenever I run the application using dotnet run command, angular serves with a different port each time.

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

I tried setting the default serve port in angular-cli.json

"defaults": {
  ..
  "serve": {
    "port": 4200
  }
}

虽然不起作用.

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

I couldn't find any documentation online, so where can I set the default port?

推荐答案

好吧,结果是,更改端口并不重要,因为我可以从URL

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.

我认为此行中指定的端口是我用来访问Angular应用程序的端口

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

来自 MSDN :

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

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.

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

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();

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

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();

这篇关于如何为ASP.NET Core Angular应用设置默认端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆