【问题标题】:Hosting .Net Core app and Angular app under IIS Default website在 IIS 默认网站下托管 .Net Core 应用和 Angular 应用
【发布时间】:2020-07-26 04:29:39
【问题描述】:

我正在使用带有角度的 ASP.net 核心样板。我尝试在 IIS localhost 下单独托管这两个应用程序的 Angular 网站和 .NET 核心网站,没有问题。
:
但是当我想在公共上部署我的应用程序时,我遇到了这个问题:
1- 我只有一个重定向到 IIS 默认网站的公共 IP,每个应用程序都有不同的端口。
**因此,前端和后端都应托管在具有不同端口的 IIS 默认网站下。

这适用于 .NET core 和 Angular 吗?**

更新

这是我的 appsetting.json

{

  "ConnectionStrings": {
    "Default": "Server=localhost\\SQLEXPRESS; Database=MyDb;User Id=admin;Password=1234"
  },
  "App": {
    "ServerRootAddress": "http://localhost:21021/",
    "ClientRootAddress": "http://localhost:4200/",
    "CorsOrigins": "http://localhost:4200,http://localhost:8080,http://localhost:8081,http://localhost:3000"
  },
  "Authentication": {
    "JwtBearer": {
      "IsEnabled": "true",
      "SecurityKey": "MyApp_C421AAEE0D114E9C",
      "Issuer": "MyApp",
      "Audience": "MyApp"
    }
  }
}

更新 2

Angular web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <remove fileExtension=".json" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
      <mimeMap fileExtension="woff" mimeType="application/font-woff" />
      <mimeMap fileExtension="woff2" mimeType="application/font-woff" />
    </staticContent>
    <!-- IIS URL Rewrite for Angular routes -->
    <rewrite>
      <rules>
        <rule name="Angular Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Angular appconfig.production.json

{
  "remoteServiceBaseUrl": "http://localhost:21021",
  "appBaseUrl": "http://localhost:4200",
  "localeMappings": [
    {
      "from": "pt-BR",
      "to": "pt"
    },
    {
      "from": "zh-CN",
      "to": "zh"
    },
    {
      "from": "he-IL",
      "to": "he"
    }
  ]
}

【问题讨论】:

  • 即使使用一个 IP 地址,您也可以在端口 80 上托管多个站点(不同的主机名)。停止使用不同端口的想法,因为这只会使事情变得复杂。
  • @LexLi 我在服务器 x 上的公共 IP 和我在服务器 Y 上托管项目,我的 X 服务器将请求重定向到 Y IIS 服务器。我不知道他们为什么这样做,但我无法改变这种情况。
  • 那么您在服务器 X 上拥有/安装了什么?通常人们在那里有一个反向代理,但你真的需要先了解你有什么。使用反向代理,您几乎可以做任何您喜欢的事情(例如docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/…)。

标签: angular asp.net-core iis web-hosting aspnetboilerplate


【解决方案1】:

只有您的 Angular 网站应该是公开的。每个 IIS 网站都需要一个唯一的端口。 https://docs.aspnetzero.com/en/aspnet-core-angular/latest/Deployment-Angular-Publish-IIS

还可以在您的 IIS 主机网站中查看 appSettings.json

"App": {
    "ServerRootAddress": "http://localhost:21021/", -> Host 
    "ClientRootAddress": "http://localhost:4200/", -> Angular
    "CorsOrigins": "http://localhost:4200" -> your domain, https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1

},

如果你已经合并了你的 Angular 和宿主项目: https://aspnetboilerplate.com/Pages/Documents/Zero/Startup-Template-Angular#merged-project

【讨论】:

  • 亲爱的 Dominik,一旦我将域添加到 CorsOrigins,前端停止工作,在检查日志文件后,我发现 CORS 策略执行失败。另外,关于第一个链接,实际上我遵循了这些步骤,但在我的场景中,我无法为我的应用程序创建任何网站,我应该做的是在 IIS 默认网站下添加两者。关于第二个链接,实际上我没有合并我单独构建每个链接的解决方案。
【解决方案2】:

这里有一些步骤:

先决条件

Client Alias=> 需要在 iis 中添加应用程序的客户端应用程序的路径名

Service Alias => 需要在 iis 中添加应用的服务应用的路径名

  1. 下载适用于 Windows 的 .NET Core 3.0 Runtime & Hosting Bundle 或者您可以根据您的项目找到确切的版本 https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.0.0-windows-hosting-bundle-installer
  2. 为了在临床内容编辑器中设置 Angular 8 基础架构,需要安装 IIS 扩展“URL Rewrite”https://www.iis.net/downloads/microsoft/url-rewrite

前端:

  1. 构建您的角度运行ng build --prod --base-href=/client-alias/

  2. 转到 IIS 中的默认网站

  3. 添加应用程序

  4. 添加别名并将物理路径指向 Angular 应用程序中的 dist 文件夹

  5. 你可以测试这个http://localhost/client-alias

    后端 (https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1#create-the-iis-site)

  6. 发布 API 项目,以便我们可以将内容复制到 API Service 的新 Web 应用程序中。

  7. 使用 .net CLR 版本创建应用程序池以无托管代码

  8. 在 IIS 中为 API 服务创建一个新的 Web 应用程序,在第 9 步中添加了应用程序池,并在第 8 步中添加了已发布文件夹的物理路径。

现在这两个应用程序以http://127.0.0.1/client-aliashttp://127.0.0.1/service-alias 运行

您可以在不打开其他端口的情况下公开您的 IP(8080)。

【讨论】:

  • 谢谢,其实我就是这么做的,但我不知道我认为与我的应用程序端口有关的问题是什么,因为前端的默认后端应用程序端口是 21021 和 4200默认网站是 8080。
  • 我更新了 1 件事。如果这不能解决您的问题,我们可以在聊天中讨论
  • 再次感谢您,实际上,我以前做过,但没有运气,我认为 Angular 和 .NET 核心应用程序配置存在问题,是的,我们可以在聊天中讨论,但我如何打开聊天会话在 StackOverflow 中?
  • 你能完成这个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2021-11-04
相关资源
最近更新 更多