【发布时间】:2017-11-14 07:11:28
【问题描述】:
我遇到了角度路由提供程序的问题。 在 app.js 路由配置中:
fmApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
$locationProvider.html5Mode(true);
$routeProvider.when('/', {
templateUrl: 'staticViews/home.html,
controller: 'fmController'
}).otherwise({ redirectTo: '/' })
})
在我的 web.config 中,我重写了 url 以将所有请求重定向到 index.html:
</system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Url to layout.html">
<match url=".*\.html|css/|img/|js/|data/|lib/|templates/|favicon.ico" negate="true" />
<action type="Rewrite" url="layout.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
在 index.html 中我有一个表单,当它提交时我希望它使用 $http 请求 服务器端的操作
$scope.submit = function(){
$http({
method:'post',
url: '/api/test',
data: JSON.stringify(
angular.copy($scope.data)
)
})
}
我的问题是当我请求 /api/test 它不调用服务器端(api-controller 和 test-action)它调用 layout.html(因为我在 web.config 中重写)。如果我不是rewrite url in web.config 并使用$locationProvider.html5Mode(true); 将出现在我的网址中,例如:http://localhost/#!/....
【问题讨论】:
-
您是否遇到任何控制台错误。
-
只是在黑暗中拍摄,但看起来您的 API 未包含在重写否定中。也许可以试试“.*\.html|api/|css/|img/|js/|data/|lib/|templates/|favicon.ico”?
-
@Dillon 哇!!那工作完美。非常感谢!
-
@vantrung1408 很高兴听到!我已将此解决方案添加为答案,以便您可以将问题标记为已解决。干杯。
标签: javascript angularjs asp.net-mvc asp.net-mvc-5