2014-09-03 146 views
6

我有一个AngularJS应用程序利用URL重写进行链接。我重写规则是这样的:IIS 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除了调用我的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> 

它工作得很好。任何想法如何我可以利用上面的清洁解决方案,仍然完成重写?

回答

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> 
+0

信号模式是一个救生员。因此而挣扎了几个小时。未来用户的另一点是,如果您在startup.cs app.start()中指定了自己的根路径,那么您将遇到问题。我不得不删除我的根路径“/ www”,只留下一切信号R映射。 – 2015-08-11 01:27:54

+0

我爱你队友!你的信号模式是一个救星²只需将其更改为管理员(我的文件夹应用程序),并像魅力一样工作。我不知道这是什么意思,因为我只是一个设计师lmao,但它的工作。所以谢谢。 – 2017-10-10 19:55:35