简体   繁体   English

Visual Studio中使用ASP.NET Web API项目的Angular项目

[英]Angular project with ASP.NET Web API project in Visual Studio

I have a solution with two projects in Visual Studio 2017. 我在Visual Studio 2017中有两个项目的解决方案。

One contains all of my Angular files for client side SPA. 一个包含客户端SPA的所有Angular文件。 While the other is a ASP.NET web api project that serves as an endpoint for http calls made by the Angular front end. 另一个是ASP.NET web api项目,它充当Angular前端进行http调用的端点。

I'm using Angular CLI and ng-serve to deploy my angular application to localhost:4200 我正在使用Angular CLI和ng-serve将我的角度应用程序部署到localhost:4200

The web api is deployed to localhost:6463 web api部署到localhost:6463

In a production environment, they would be under the same domain. 在生产环境中,它们将位于同一个域中。 However, in development they are not in the same domain as the port differs for localhost. 但是,在开发过程中,它们与localhost的端口不同,不在同一个域中。 Forcing me to implement CORS as the Angular app has to talk to the web api. 迫使我实现CORS,因为Angular应用程序必须与web api交谈。

To me, it seems less than ideal to implement CORS for the purposes of development only. 对我而言,仅为了开发目的而实施CORS似乎并不理想。

Is there a better structure? 有更好的结构吗? Or is there anything that I am missing? 还是有什么我想念的?

Simple Answer 简单的答案

No. See the proxy section below for updated answer. 不。 请参阅下面的代理部分以获取更新的答案

You will need to enable CORS. 您需要启用CORS。 According to the Official Microsoft Documentation for ASP.NET Core , under the section about Cross-Origin Requests (CORS) they provide the definition of "same origin" : 根据ASP.NET核心官方Microsoft文档 ,在有关跨源请求(CORS)的部分中,它们提供了“相同来源”的定义:

What is "same origin"? 什么是“同源”?

Two URLs have the same origin if they have identical schemes, hosts, and ports. 如果两个URL具有相同的方案,主机和端口,则它们具有相同的来源。 (RFC 6454) (RFC 6454)
These two URLs have the same origin: 这两个URL具有相同的来源:

  • http://example.com/foo.html
  • http://example.com/bar.html

These URLs have different origins than the previous two: 这些URL的起源与前两个不同:

  • http://example.net - Different domain http://example.net - 不同的域名
  • http://www.example.com/foo.html - Different subdomain http://www.example.com/foo.html - 不同的子域名
  • https://example.com/foo.html - Different scheme https://example.com/foo.html - 不同的方案
  • http://example.com:9000/foo.html - Different port ⇦ ⇦ ⇦ ⇦ Your Issue http://example.com:9000/foo.html - 不同的端口⇦⇦⇦⇦你的问题

Note: 注意:
Internet Explorer does not consider the port when comparing origins. 比较源时,Internet Explorer不考虑端口。

How to enable CORS 如何启用CORS

A quick way to enable CORS for you case: 为您启用CORS的快速方法:

Startup.cs Startup.cs

app.UseCors(builder => builder.WithOrigins("localhost"));

Update: According to this tutorial , it might be possible to do it without CORS. 更新:根据本教程 ,可以在没有CORS的情况下完成。

Proxy to the API 代理到API

You need to create a file called proxy.conf.json in the Frontend directory: 您需要在Frontend目录中创建一个名为proxy.conf.json的文件:

{
  "/api": {
    "target": "http://localhost:65498",
    "secure": false
  }
}

The target value contains a port number. 目标值包含端口号。 If you're using Visual Studio, you can read it from Backend project properties. 如果您使用的是Visual Studio,则可以从后端项目属性中读取它。

在此输入图像描述

This will pass all the API requests to the running ASP.NET Core application. 这会将所有API请求传递给正在运行的ASP.NET Core应用程序。

The last thing we need to do here is to modify npm start script, so it uses the proxy configuration. 我们在这里需要做的最后一件事是修改npm start脚本,因此它使用代理配置。 Open package.json in the Frontend project, find the scripts section and modify start command to: 在前端项目中打开package.json ,找到scripts部分并将start命令修改为:

"start": "ng serve --proxy-config proxy.conf.json"

To work on the app, you need to start both, ng dev server and ASP.NET application. 要处理应用程序,您需要启动ng dev服务器和ASP.NET应用程序。 The first one is started with the command npm start executed from the Frontend directory. 第一个是从前端目录执行的命令npm start Backend app can be started from the Visual Studio or also command line with dotnet watch run . 后端应用程序可以从Visual Studio或dotnet watch run命令行dotnet watch run If you use the command line version, be careful about the port it uses and set it up properly in the proxy config file. 如果使用命令行版本,请注意它使用的端口并在代理配置文件中正确设置它。 The watch command in dotnet watches for changes in the application and rebuilds it whenever you change something. dotnet中的watch命令会监视应用程序中的更改,并在您更改内容时重建它。

To enable CORS in a ASP.NET Web API 2 site you can follow the instructions here . 要在ASP.NET Web API 2站点中启用CORS,您可以按照此处的说明进行操作。

To start you have to add the CORS NuGet package with the following command in your Package Manager Console: 首先,您必须在Package Manager控制台中使用以下命令添加CORS NuGet包:

Install-Package Microsoft.AspNet.WebApi.Cors

Then add the following line to your WebApiConfig.Register method: 然后将以下行添加到WebApiConfig.Register方法:

config.EnableCors();

Then, edit your api controller class to include: 然后,编辑您的api controller类以包括:

using System.Web.Http.Cors;

and add the CORS attribute before the controller class declaration: 并在controller类声明之前添加CORS属性:

[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]

Solution for Chrome via browser extension Chrome浏览器扩展解决方案

If you don't want to change anything in your backend and you are using Chrome for development, you can use an extension like Moesif Origin & CORS Changer . 如果您不想更改后端中的任何内容并且使用Chrome进行开发,则可以使用Moesif Origin和CORS Changer等扩展程序。

It allows you to override the Origin and Access-Control-Allow headers of the browser's request. 它允许您覆盖浏览器请求的OriginAccess-Control-Allow标头。

Download and install the extension, then activate it via the icon in the top right of Chrome. 下载并安装扩展程序,然后通过Chrome右上角的图标将其激活。 Right click the icon and click Options . 右键单击图标,然后单击“ Options Now you can modify the Origin and Access-Control-Allow- headers to your liking. 现在,您可以根据自己的喜好修改OriginAccess-Control-Allow-标头。

This is strictly for development on localhost, DO NOT forget to deactivate the extension when you are done! 这是严格用于localhost的开发,不要忘记在完成后停用扩展!


Here is an example request i made using ASP.NET Core and Angular CLI. 以下是使用ASP.NET Core和Angular CLI进行的示例请求。 The Angular dev server runs on port 4200 and the backend on port 53632 . Angular dev服务器在端口4200上运行,后端在端口53632

Request without enabling the extension: 请求未启用扩展程序: 请求没有扩展名

Enable the extension and override origin: 启用扩展和覆盖原点: 启用扩展程序

and now the Origin -Header has been changed: 现在Origin -Header已被更改: 请求扩展名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Visual Studio 2012 Express for Web将Web API和文件夹引用添加到空ASP.NET项目 - Adding Web API and folder references to empty ASP.NET project with Visual Studio 2012 Express for Web 在 ASP.NET Web API 2 项目中为 Angular 设置路由 - Setting up Routing for Angular in a ASP.NET Web API 2 project Web矩阵与Visual Studio有何不同。它是否比Visual Studio更有效地开发ASP.NET Web Project? - How web matrix differ from Visual Studio .is it more efficient than Visual Studio to develop ASP.NET Web Project? 如何在 Visual Studio 中创建和部署 Mono ASP.NET web 项目? - How to create and Deploy a Mono ASP.NET web project in Visual Studio? 如何在 Visual Studio Asp.net 项目中为不同的团队成员设置不同的 web 服务器配置? - How to set up different web server config for different team members in Visual Studio Asp.net Project? 无法使用Visual Studio 2017将ASP.NET Core Angular项目发布到Azure - Failed to publish ASP.NET Core Angular Project to Azure with Visual Studio 2017 在 Visual Studio 2019 社区中将 Azure API 链接到 C# ASP.Net 项目的问题 - Issue linking Azure API to C# ASP.Net project in Visual Studio 2019 Community Visual Studio-ASP.NET MVC 5-默认Azure AD身份验证项目和Microsoft Graph API - Visual Studio - ASP.NET MVC 5 - Default Azure AD Authentication Project and Microsoft Graph API Visual Studio 2019 ASP.NET Core API 项目模板坏了? - Visual Studio 2019 ASP.NET Core API project template broken? 从ASP.NET 4网站项目迁移到Visual Studio 2010中的Web应用程序项目是否导致所有控件引发错误? - Migrating from ASP.NET 4 website project to web application project in Visual Studio 2010 Causing all controls to throw error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM