【问题标题】:Can't request to ASP.NET mvc action when using AngularJS Routeprovider使用 AngularJS Routeprovider 时无法请求 ASP.NET mvc 操作
【发布时间】: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


【解决方案1】:

只是在黑暗中拍摄,但看起来您的 API 未包含在重写否定中。也许试试

<match url=".*\.html|api/|css/|img/|js/|data/|lib/|templates/|favicon.i‌​co" negate="true" />

可能吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-06
    • 2015-02-14
    • 2013-04-16
    • 2012-05-15
    • 2018-11-23
    • 1970-01-01
    • 2013-08-08
    • 2014-02-22
    相关资源
    最近更新 更多