在IIS默认网站下托管.Net Core应用程序和Angular应用程序

时间:2020-04-13 15:09:38

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

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

但是当我想在公共场所部署我的应用程序时,遇到了这个问题:
1-我只有一个公共IP重定向到IIS默认网站,并且每个应用程序都有不同的端口。
**因此,前端和后端都应使用不同的端口托管在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>

角度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"
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

以下是一些步骤:

前提条件

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

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

  1. 下载适用于Windows的.NET Core 3.0运行时和主机捆绑包,或者根据项目找到确切的版本 https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.0.0-windows-hosting-bundle-installer
  2. 要在Clinical Content Editor中设置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. 添加别名并在角度应用程序中将物理路径指向您的 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),而无需打开其他端口。

答案 1 :(得分:1)

只有您的有角度的网站应该是公开的。每个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和Host项目: https://aspnetboilerplate.com/Pages/Documents/Zero/Startup-Template-Angular#merged-project

相关问题