我正在尝试用Web后端来建立一个单一的页面应用程序。我使用"OWIN WebAPI SPA模板“来启动我的项目。此模板默认为解决方案根目录中的公用/文件夹服务的静态文件。我想支持html5网址,IE。localhost/madeUpPath,它应该访问index.html页面。为了做到这一点,我在Web.config文件中设置了一个重写规则:
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="api/" ignoreCase="true" negate="true" />
<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>我遇到的问题是,由于静态文件驻留在/public/ REQUEST_FILENAME}中,{}认为该文件相对于/,而url localhost/main.css被重写为localhost/
我试着把这个规则改为:
<add input="public/{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="public/{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />但这不起作用。我怎样才能达到我想要的结果?谢谢。
编辑:,我发现了一些似乎有用的东西,但并不是我想要的。
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="([a-zA-Z0-9-_.\/]+\.(css|js|less|json|jpeg|jpg|png|gif|svg|xml|html))$" />
<action type="Rewrite" url="/public/{R:0}" />
</rule>
<rule name="Second 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="/" />
</rule>
</rules>
</rewrite>发布于 2015-09-29 06:05:21
首先,这段代码并不是因为您使用OWIN服务器来处理静态文件:
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>请您使用下面的代码:
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="^((?!(api|\.)).)*$" />
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>正则表达式使我们能够忽略api和任何文件路径。
发布于 2018-04-24 13:44:39
我们还使用Owin为子文件夹中的内容提供服务,以下内容非常适合我们:
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsDirectory" ignoreCase="true" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>它非常类似于这句话,但我将它们的{REQUEST_URI}变量更改为{URL},这样即使它们有查询字符串,也会忽略现有文件,而查询字符串对于缓存破坏资源(如site.css?ec99043d9af49f2bd7c2 )是必要的。
编辑:我也刚刚找到了此Microsoft示例,经过一些调整可能也适用于这个场景,但我还没有亲自测试过它:
<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" />
</conditions>
<action type="Rewrite" url="/MyApp/" />
<!--<action type="Rewrite" url="/" />-->
</rule>
</rules>
</rewrite>https://stackoverflow.com/questions/32387089
复制相似问题