我正在使用带角度的ASP.net核心样板。我尝试将两个应用程序分别托管在Angular网站和IIS localhost下的.NET Core网站上,都没有问题。
:
但是当我想在公共场所部署我的应用程序时,遇到了这个问题:
1-我只有一个公共IP重定向到IIS默认网站,并且每个应用程序都有不同的端口。
**因此,前端和后端都应使用不同的端口托管在IIS默认网站下。
更新
这是我的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"
}
]
}
答案 0 :(得分:3)
以下是一些步骤:
前提条件
Client Alias =>需要在iis中添加应用程序的客户端应用程序的路径名
服务别名=>需要在iis中添加应用程序的服务应用程序的路径名
前端:
构建角行程 ng build --prod --base-href = / client-alias /
转到IIS中的默认网站
添加应用程序
添加别名并在角度应用程序中将物理路径指向您的 dist 文件夹
发布API项目,以便我们可以将内容复制到API服务的新Web应用程序。
使用.net CLR版本创建应用程序池以无托管代码
在IIS中,为API服务创建一个新的Web应用程序,并在步骤9中添加应用程序池,并在步骤8中添加已发布文件夹的物理路径。
现在,这两个应用程序分别以http://127.0.0.1/client-alias和http://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