IIS URL 将 Service1.svc 重写为 api

IIS URL Rewrite Service1.svc to api

我正在尝试使用 IIS URL 重写重写:

http://localhost/RESTDemo/Service1.svc/Personshttp://localhost/RESTDemo/api/Persons

我的 Web.config 中有以下配置:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Change 'Service1.svc' to 'api'" stopProcessing="true">
                <match url="^(.*)/RESTDemo/api/(.*)$" />
                <action type="Rewrite" url="{R:1}/RESTDemo/Service1.svc/{R:2}" logRewrittenUrl="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

当我 运行 IIS 中的 测试模式 时,它似乎工作正常

但是当我测试 POST 到 http://localhost/RESTDemo/api/Persons 时,我从 IIS 得到了 404.0 Not Found。然后,当我尝试 运行 与 http://localhost/RESTDemo/Service1.svc/Persons 相同的 POST 时,它起作用了。

我已经尝试检查 C:\inetpub\logs\LogFiles\W3SVC1 下的日志,但没有看到任何有用的信息,即使我已经设置 logRewrittenUrl="true" 我只能在我的 IIS 日志中看到这个:

2021-04-20 10:18:48 127.0.0.1 POST /RESTDemo/api/persons - 80 - 127.0.0.1 Apache-HttpClient/4.5.5+(Java/12.0.1) - 404 0 2 1

我的问题是..

  1. 我做错了什么?
  2. 为什么我在日志文件中看不到重写的URL?

这是因为你的重写规则有问题,你可以试试下面的重写规则:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Change 'Service1.svc' to 'api'" stopProcessing="true">
                <match url="^(RESTDemo)/api/(.*)$" />
                <action type="Rewrite" url="RESTDemo/Service1.svc/{R:2}" logRewrittenUrl="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

这是我的演示: