使用Web Api控制器参数映射AngularJs QueryString

时间:2015-09-22 20:02:32

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

这是我的Angularjs控制器代码:

$.ajax({
    url: "http://localhost:6543/linefollower/7/send_result", 
    type: "POST",
    data: JSON.stringify({"results": [... "team_name": "IT Vennad", "id": 57}]}),
    contentType: "application/json; charset=utf-8",
    dataType: "json"
}

app.controller('listAdsController', ['$scope', 'myService', '$routeParams', '$location', function ($scope, myService, $routeParams, $location) { myService.getAdsByCategory({categoryId : $routeParams.categoryId}); $scope.totalItems = 64; $scope.pageChanged = function () { var path = $location.path(); $location.path(path).search('page', $scope.currentPage); }; $scope.maxSize = 10; $scope.bigTotalItems = 175; $scope.currentPage= 1; $scope.itemsPerPage = 50; }]); 函数中,我尝试使用如下的查询字符串根据页码更改我的Url:

  

//本地主机/类别/特性/ 4 /房子换租?页= 2

以下是该网址的路由配置:

pageChanged

现在,下面是我的Web Api 2控制器:

$routeProvider.when("/category/:category/:categoryId/:urlName", {
        controller: "listAdsController",
        templateUrl: "app/views/category-ads.html"
    });

问题是, [HttpGet] public IHttpActionResult GetAllByCategory([FromUri] int categoryId, int? page = null) { //Code } 参数始终始终为page,无论我传递的是什么。你能指导一下我在这里缺少的东西吗?

1 个答案:

答案 0 :(得分:0)

问题非常小。我只是将查询字符串传递给url,但是没有从routeParam获取它。

解决方案是:

$scope.categoryAds = jetService.getAllByCategory({ categoryId: $routeParams.categoryId, page: $routeParams.page });

您需要从查询字符串中获取page将其传递给您的服务。

相关问题