-1

我们正在为我们的微前端创建一个新架构,并且我们希望避免使用另一个框架来处理它,比如single-spa。我们公司目前将 IIS 用于其他产品,我们需要保持这种方式。

因此,为了让多个 SPA 响应同一个地址,我们只需将我们构建的 SPA 放在 IIS 的内容目录中,如下所示:

IIS 根文件夹:

  • 温泉1
  • 温泉2

我可以正常访问myhost/spa1myhost/spa2浏览他们自己的内部路线(我们使用的是 Angular)。

问题是,我们希望 URL 没有#,并且我们需要告诉 IIS 将任何子路由重定向到它的 SPA 主路由。

我们有一些东西,它适用于myhost/spa1, myhost/spa1/detail,但不适用于myhost/spa1/detail/1。比方说,第三级。

有人可以帮我解决这个问题吗?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Rule" 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="{R:1}/" />
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>
4

2 回答 2

1

尝试使用此 URL 重写规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Rule" 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="{R:1}/{R:2}" />
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>

在此处输入图像描述

于 2021-01-14T02:55:43.737 回答
0

经过几次试验和另一个要求(我们需要在 URL 中选择一个“公司”),我想出了一些运行良好的方法:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Application Hub Rule" stopProcessing="true">
                    <match url="(hub)(/?.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}/" />
                </rule>
                <rule name="CRM Customer Rule" stopProcessing="true">
                    <match url="(crm/customer)(/?.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}/" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

我知道,这是非常独特的要求。但事实证明,这些规则运作良好。一旦用户进入my-host/COMP_CODE/hub,我的 Angular 应用程序就会处理 URL 更改,因为之后的所有内容都my-host/COMP_CODE/hub/**/**被 IIS 重定向到集线器的根文件夹。一旦你输入my-host/COMP_CODE/crm/customer了它的子路由,IIS 会将所有内容重定向到客户的根文件夹,并且 Angular 会处理 URL 更改。

唯一的问题是,在您的 AngularRouterModule.forRoot()配置中,您需要确保 Angular 的路由器能够理解path: ':db/hub'(除其他外)。

感谢那些试图提供帮助的人。

于 2021-01-20T18:48:10.370 回答