【发布时间】:2014-10-27 22:51:12
【问题描述】:
我有一个使用 URL 重写进行链接的 AngularJS 应用程序。我的重写规则如下:
<rewrite>
<rules>
<rule name="MainRule" 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_FILENAME}" matchType="Pattern" pattern="^api/(.*)" negate="false" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
我在 SO:How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? 上找到了这个解决方案,并进行了一项修改以允许我的 API 通过。
基本上,我想将我的所有请求重定向到Default.cshtml,除了对我的 Web API 的调用。当我在基本 URL 上加载应用程序时,这很有效,例如:localhost/myapp。但是,如果我在有角度的路线上重新加载页面,例如:localhost/myapp/dashboard,它会失败说:
<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost/myapp/dashboard'.</Message>
<MessageDetail>No type was found that matches the controller named 'dashboard'.</MessageDetail></Error>
我有一个解决办法:
<rewrite>
<rules>
<rule name="Default" stopProcessing="true">
<match url="^(?!lib|api|dist|assets|app/|bower|common|main|signalr|templates|bower_components/).*" />
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
而且效果很好。有什么想法可以利用上述更清洁的解决方案并仍然完成重写吗?
【问题讨论】:
标签: iis url-rewriting