这是场景。 我有一个 Angular 应用程序和一个托管在 IIS 上的 .net 8 Web API。 .net wep api 作为应用程序添加到站点,并以“dotnet”作为别名
我需要 domain.com/dotnet 才能通过,但其他所有内容都需要重写为 ./index.html
当我去 domain.com/dotnet/someRequest 服务器只是重定向到 angualr 应用程序,而不是从 api 提供 json 数据。
这是我当前的IIS重写规则
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern=".*/dotnet/*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
我也试过这个
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="DOTNET" stopProcessing="true">
<match url=".*/dotnet/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="UNENCODED_URL" appendQueryString="false" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
如果我删除所有重写规则,那么我可以访问 domain.com/dotnet/getRequest,它将返回 json 数据,我可以通过访问 domain.com 访问 angualar 应用程序。
但是如果我刷新页面并且我的网址类似于domain.com/homepage,那么 IIS 无法找到该页面。
我的目标是传递所有 domain.com/dotnet/* 请求,并将其他所有内容重写到 ./index.html 以避免刷新问题。
任何帮助将不胜感激!
您可以尝试以下规则:
<rewrite>
<rule name="DotNetPassThrough" stopProcessing="true">
<match url="^dotnet/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="None" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
或
<rewrite>
<rules>
<rule name="DotNetPassThrough" stopProcessing="true">
<match url="dotnet/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="None" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
此规则将帮助您按原样服务 donet 请求,并且还将提供有角度的路线。