如何在IIS上部署Angular和.NET Core Web API? [英] How To Deploy Angular And .NET Core Web API On IIS?

查看:260
本文介绍了如何在IIS上部署Angular和.NET Core Web API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在IIS上有两个用于angular_client和web_api的网站,已安装了托管捆绑包,并且已经授予IUSR,Network,Network Service和Everyone等用户许可.

We have two web sites on IIS for both angular_client and web_api, the hosting bundle is installed and the permission to users such as IUSR, Network, Network Service and Everyone, is already granted.

单独导航到每个站点,即在API中,调用 http://localhost:51975/api/user ,显示所有用户的列表.

The navigation to each site seperately works, i.e in the API, the call http://localhost:51975/api/user, results in list of all users.

问题是Angular登录页面无法与API通信以验证用户身份

The problem is that Angular login page fails to communicate with the API to authenticate users:

OPTIONS http://localhost:51975/api/user/authenticate net::ERR_CONNECTION_REFUSED

我试图将与API关联的IP修改为静态IP,即10.1.xy,然后告诉angular调用 http://10.1.xy:51975/api/user/authenticate .

I tried to modify the IP associated with the API to a static one, i.e 10.1.x.y and then tell angular to call http://10.1.x.y:51975/api/user/authenticate instead.

在API中,我启用了将日志发送到浏览器以及日志文件夹的功能,它显示的唯一日志是:

In the API I enabled sending the log to browser and also to logs folder, the only log it shows is:

Hosting environment: Production
Content root path: C:\inetpub\wwwroot\api
Now listening on: http://127.0.0.1:39834
Application started. Press Ctrl+C to shut down.
Application is shutting down...

为什么它在其他端口而不是相关的51975上进行监听?

Why it is listening on other port rather than the associated one 51975?

为什么从Angular到API的连接被拒绝?

Why the connection is refused from the Angular to the API?

为什么应用程序正在关闭?

Why the application is shutting down?

这是正确的方法吗?

更新1

仅在服务器 上,应用程序可以正常运行,并具有以下配置:

On the server ONLY, the application works fine, with the following configurations:

  1. 角度位于10.x.y.z:4200,

  1. Angular is on 10.x.y.z:4200,

API 10.x.y.z:51975,

API 10.x.y.z:51975,

问题是,当我尝试使用公共ip或主机名时,我仍然只能访问angular而不是Web API,我试图为api分配主机名,但仍无法正常工作!

The problem is that when I try to use the public ip or my host name, I still can access only the angular but not the Web API, I tried to assigne the host name for the api, and it is not working yet!

更新2

我还允许在我的主机名域上同时使用端口4200和51975.

I have also allowed both of the ports 4200 and 51975 on my host name domain.

更新3

我将Web API托管在IIS上,API和Angular都在同一台服务器上,但网站不同.

I am hosting the Web API on IIS, both API and Angular are on the same server but diferent websites.

我删除了有关CORS已修复的声明,因为在浏览器控制台上仍然有警告,这是错误消息:

I removed the statement about CORS fixed, because I still have a warning on the browser console, here is the error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://{public IP}:51975/api/user/authenticate. (Reason: CORS request did not succeed).[Learn More]

Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://{public IP}:51975/api/user/authenticate", ok: false, name: "HttpErrorResponse", 
 message: "Http failure response for http://{public IP}:51975/api/user/authenticate: 0 Unknown Error", error: error }


OPTIONS Http failure response for http://{public IP}:51975/api/user/authenticate net::ERR_CONNECTION_TIMED_OUT

关于CORS,这是我的配置,在本地运行良好:

About CORS, here is my configuration, it works fine locally:

 //In ConfigureServices method
 services.AddCors();

 //In Configure method
 app.UseCors(options => options.WithOrigins("http://localhost:4200/").AllowAnyMethod().AllowAnyHeader());

谢谢

推荐答案

CORS错误说明了您需要了解的所有内容.如果还使用DNS名称访问,则还需要为公用IP和DNS配置CORS.必须将其配置为与访问Angular应用程序使用的网址完全相同.

The CORS error explains everything you need to know. You need to configure CORS also for the public IP and DNS if you use also access by DNS name. It has to be configured for the exact same URL you use to access the Angular application.

app.UseCors(options => 
   options.WithOrigins(
      "http://localhost:4200/",
      "http://{public IP}:{public port}/",
      "http://{public DNS name}:{public port}/"
   ).AllowAnyMethod().AllowAnyHeader());

您仅需要Angular客户端地址的CORS设置,因为Angular应用程序需要获得许可才能向Web API发出CORS请求.它从公用地址进行调用,因此localhost配置无济于事,仅当您使用本地主机在本地访问它时,此功能才有用.

You need only CORS settings for the Angular client addresses because the Angular application needs to obtain permission to make CORS requests to your Web API. It calls from the public address so the localhost configuration does not help this just helps when you access it locally with localhost.

这里是有关CORS的角度文档的链接 Microsoft文档如何启用CORS 详细说明.

这篇关于如何在IIS上部署Angular和.NET Core Web API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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