为Web API URL配置Angularjs路由

时间:2015-05-11 13:26:30

标签: javascript angularjs iis asp.net-web-api iis-7

我开发了angularjs应用程序和Asp.net Web Api。两者都托管在IIS服务器的端口80中。问题是我无法访问web api URL,因为angularjs将重定向API调用路由到应用程序的根目录。

Angularjs Routing

.when('/home', {
    templateUrl: baseUrl + 'home.html',
       controller: 'homeCtrl'
    })
    .otherwise({
       redirectTo: '/' //Because of this web api call redirect to root of the web app 
    });

我可以按预期配置运行Web Api请求的角度路由方法吗?

EDITED

我在wb.conf上使用了一些重写规则

<rule name="RemoveTrailingSlash" stopProcessing="true">
                            <match url="(.*)/$" />
                            <conditions>
                              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                            </conditions>
                            <action type="Redirect" redirectType="Permanent" url="{R:1}" />
                          </rule>   

                          <rule name="HotelRedirectRulesLenon1" patternSyntax="ECMAScript" stopProcessing="true">
                            <match url=".*" />
                            <conditions logicalGrouping="MatchAny">
                              <add input="{HTTP_HOST}" pattern="^example.(com|net|com.au)$" />           
                            </conditions>
                            <action type="Redirect" url="http://www.example.org" />
                          </rule>
                          <rule name="HotelRedirectRulesLenon2" patternSyntax="ECMAScript" stopProcessing="true">
                            <match url=".*" />
                            <conditions logicalGrouping="MatchAny">            
                              <add input="{HTTP_HOST}" pattern="^www.example.(com.au)$" />
                            </conditions>
                            <action type="Redirect" url="http://www.example.org/{R:0}" />
                          </rule>       
                        <rule name="AngularJS" 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="/index.html" />
                        </rule>
                        <rule name="Prerender" stopProcessing="true">
                            <match url="^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(.*)" ignoreCase="false" />
                            <conditions logicalGrouping="MatchAny">
                                <add input="{HTTP_USER_AGENT}" pattern="baiduspider|facebookexternalhit|twitterbot" />
                                <add input="{QUERY_STRING}" pattern="_escaped_fragment_" ignoreCase="false" />
                            </conditions>
                            <action type="Rewrite" url="http://example/{R:2}" />
                        </rule>

1 个答案:

答案 0 :(得分:1)

只需添加以下新的重写规则即可解决我的问题。

<rule name="AngularJS" 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}" pattern="^/(webApi)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
相关问题