我正在尝试将我的 Angular 应用程序和 API 托管在 Azure 上的同一个 Web 应用程序中。 但我在访问我的 api 时遇到一些困难。
文件夹结构如下:
├───api
│ └───bin
│ └───web.config <== API web.config
├───index.html
├───web.config <== Angular web.config
├───...
└───assets
├───flags
├───i18n
├───icons
└───images
所以我需要告诉我的 Angular web.config
将所有 /api
调用重定向到 api。同时仍然恢复对根文件夹中的 index.html
的所有其他调用
但我似乎无法正确理解,这是我当前的 web.config。但这会将我的 api 调用重定向到客户端的 index.html。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<remove fileExtension=".ttf" />
<mimeMap fileExtension=".woff" mimeType="font/woff" />
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
<mimeMap fileExtension=".ttf" mimeType="font/ttf" />
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<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>
<!-- gzip compression -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="font/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="font/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
<location path="index.html">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
这会产生以下 list
- 加载 Angular 应用程序 => TRUE
- 加载文件(favicon、main.js、ngsw-worker.js)=> TRUE
- 加载manifest.webmanifest => TRUE
- 加载 API => FALSE
有人知道有效的 web.config
吗?
更新:通过将其添加到静态内容来修复manifest.webmanifest问题
最佳答案
我在 Azure 应用服务中运行了相同的设置,并且在尝试让一切正常工作时也遇到了问题。最后,我遇到的问题是我的 SPA 应用程序 web.config 被 API 应用程序继承。当尝试调用我的 API 应用程序时,这会导致 405 错误。
我的解决方案是调整我的 web.config 并关闭继承。
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
关于angular - IIS 允许路由到虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589992/