繁体   English   中英

Hosting.Net Core app 和 IIS 下的 Angular app 默认网站

[英]Hosting .Net Core app and Angular app under IIS Default website

我正在使用 ASP.net 核心样板和 angular。 我尝试在 IIS localhost 下分别托管 Angular 和 .NET 核心的两个应用程序网站,没有问题。

但是当我想在公共上部署我的应用程序时,我遇到了这个问题:
1-我只有一个公共 IP 重定向到 IIS 默认网站,每个应用程序都有不同的端口。
**因此,前端和后端都应托管在具有不同端口的 IIS 默认网站下。

是否适用于 .NET 内核和 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"
    }
  ]
}

以下是一些步骤:

先决条件

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

服务别名 => 需要在 iis 中添加应用程序的服务应用程序的路径名

  1. Download .NET Core 3.0 Runtime & Hosting Bundle for Windows or you can find exact version as per your project https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.0.0-windows-托管捆绑安装程序
  2. 对于在临床内容编辑器中设置 Angular 8 基础设施,需要安装 IIS 扩展“URL Rewrite” httpsurl://www.iis.net/downloads/microsoft

前端:

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

  2. Go 到默认 Web 站点在 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 服务的新 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)。

只有您的 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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM