我有一个AngularJS应用程序,它利用URL重写进行链接。我的重写规则如下:
<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
EXCEPT调用我的Web API。当我在基本网址上加载应用程序时,这会很有效: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>
它工作正常。任何想法如何我可以利用上面的清洁解决方案,仍然完成重写?
答案 0 :(得分:6)
我需要更改模式,并在几个地方将{REQUEST_FILE}
更改为{REQUEST_URI}
。在这里看我的演示:
<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_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>