我的前端位于 Aurelia,我正在尝试添加 .NET Core 后端。我的后端应该向前端发送一些数据,并接收前端提交的数据。我尝试了以下方法:
mkdir app
cd app
dotnet new webapi
au new --here
这工作正常,但后端正在监听端口 5000,而我的前端正在监听端口 8080。这是添加 .NET Core 后端的正确方法吗?同时拥有前端和后端运行在不同的端口上?
最佳答案
Is this the right way to add a .NET Core back-end
如果您想为 Aurelia
添加新的 ASP.NET Core
项目,则无需创建自己的工作流程。 您不必创建 ASP.NET Core
项目。
只需使用au new
(不使用dotnet new webapi
):
PS aurelia-app-hello> au new helloworld --here No Aurelia project found. _ _ ____ _ ___ __ _ _ _ _ __ ___| (_) __ _ / ___| | |_ _| / _` | | | | '__/ _ \ | |/ _` | | | | | | | | (_| | |_| | | | __/ | | (_| | | |___| |___ | | \__,_|\__,_|_| \___|_|_|\__,_| \____|_____|___| Which module loader / bundler would you like to use? 1. Webpack (Default) A powerful and popular bundler for JavaScript 2. CLI's built-in bundler with RequireJS RequireJS is a mature and stable module loader for JavaScript. 3. CLI's built-in bundler with SystemJS SystemJS is Dynamic ES module loader, the most versatile module loader for JavaScript [Webpack]> What platform are you targeting? 1. Web (Default) The default web platform setup. 2. ASP.NET Core A powerful, patterns-based way to build dynamic websites with .NET. [Web]> 2
选择第二个选项,au-cli
将为您创建一个ASP.NET Core
项目。它还会自动设置所有配置。
is it good practice to have both your front and back-end running on different ports?
别担心。你的方向是正确的。如果你查看source code of SPA通过 ASP.NET Core 团队,你会发现他们也在做和你一样的事情。当有传入的相关消息时,ASP.NET Core 服务器将简单地将其代理到开发服务器。
例如,the default project template for creating ASP.NET Core application with Angular使用 angular-cli 在端口上启动前端开发服务器,这与 ASP.NET Core 监听的端口不同。
顺便说一句,“前端”项目仅在开发时运行。无需关心性能。
关于.net - 将 .NET Core 后端添加到 Aurelia 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53710963/