javascript - 为 Web API URL 配置 Angularjs 路由

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

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

Angularjs 路由

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

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

已编辑

我在我的 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>

最佳答案

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

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

关于javascript - 为 Web API URL 配置 Angularjs 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30168938/

相关文章:

javascript - 如何使用选择器过滤 `each` 中的 cheerio 对象?

javascript - 在 d3.js 中的点之间绘制线

javascript - 类型错误 : Cannot read property 'open' of undefined modal

c# - 如何在防火墙后设置安全的 WCF 服务?

javascript - React 在使用 React.Children.map 时会自动处理键吗?

javascript - 使用angularjs将json对象传递给python

angularjs - 在 Angular 初始化结束时执行代码,并显示ngCloak

angularjs - 如何使用 angular.js 检查元素的类型

iis - 找不到 IIS AppPool 标识

asp.net - Application_End 全局.asax