将前端Angular 5和后端Web API代码分开的简单方法? [英] Simple way to keep Front-end Angular 5 and back-end Web API code separate?

查看:118
本文介绍了将前端Angular 5和后端Web API代码分开的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Angular 5应用,其中包含许多活动部件(服务,引导程序,Angular Material,内部和外部JS脚本等).现在,我也想向该应用程序添加数据库连接(仅适用于MS SQL Server).该应用程序已使用Visual Studio Code开发.

I have developed an Angular 5 app with plenty of moving parts (Services, Bootstrap, Angular Material, internal+external JS scripts, etc). Now I want to add database connectivity to this app as well (MS SQL Server only). The app has been developed in Visual Studio Code.

我正在从MSDN中关注本教程为了了解如何将此应用程序与SQL Server数据库连接.我相信我需要Web API和.NET Core来支持连接性,而且我无法找到一种方法,而无需从任何一个VS Code中重新开始(例如

I am following this tutorial from MSDN in order to learn how to connect this app with an SQL Server database. I believe I'll need Web API and .NET Core support for connectivity, and I can't find out a way to do that without starting from scratch in either of the VS Code (e.g. here) or Visual Studio (like in the referenced article) since almost all the articles/tutorials I could find on the topic begin from scratch. The way I see it, creating a new Angular + Core project in Visual Studio and migrating the app logic there file-by-file is not a feasible solution for me.

我的问题是:

  • 是否有一种简单的方法来创建基于单独的基于Web API的后端应用程序,该应用程序可从SQL Server数据库中提供数据以供前端Angular 5应用程序使用?还是所有的.NET Core,Web API和Angular前端支持都可以从同一应用程序呈现?
  • 如果没有,是否有简单/官方的方法向VS Code内置的Angular 5应用中添加.NET Core和Web API功能?
  • Is there a simple way to create a separate Web API-based back-end app which serves data from my SQL Server database for the front-end Angular 5 app to consume? Or would all the .NET Core, Web API, and Angular front-end support be rendered from the same app?
  • If not, is there a simple/official way to add .NET Core and Web API functionality to an Angular 5 app built in VS Code?

推荐答案

我建议您创建一个单独的项目,其中包含带有ASP.NET Core的REST-Endpoint.

I propose you to create a separate project which contains the REST-Endpoint with ASP.NET Core.

在这里您可以找到一个简单的教程:

Here you find a simple tutorial:

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api

使用此解决方案,您的服务器与客户端断开了连接.将来也许您想使用除angular之外的其他功能,或者您想创建其他客户端(例如,本机移动应用程序).在这种情况下,您不必更改服务器项目.

With this solution your server is decoupled from client. Maybe in future you like to use something other than angular or you like to create an other client (for example a native mobile app). In this case you do not have to change your server project.

(在您使用Visual Studio的情况下,请看一下项目模板.有您需要的一切.

(In the case you are working with visual studio take a look to the projecttemplate. There is every thing you need.

这篇关于将前端Angular 5和后端Web API代码分开的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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