AngularJS将参数传递给Web Api Get

时间:2016-11-30 09:42:46

标签: angularjs asp.net-web-api

我试图将对象参数从angularJs控制器传递给Web Api 2 Get Method控制器。

如果我像对象一样传递对象,它会起作用,但我不喜欢这个解决方案。

[HttpGet]
public IEnumerable<SearchVenueByParameters_Result> Get(string parameter)
    {
        ObjSearch obj_search = new JavaScriptSerializer().Deserialize<ObjSearch>(parameter);

我想试试这个:

[HttpGet]
    public IEnumerable<SearchVenueByParameters_Result> Get(ObjSearch obj_search)

ObjSearch课程:

public class ObjSearch {
    public string aAMSID { get; set; }
    public string venueName { get; set; }
    public string address { get; set; }
    public byte? businessSK { get; set; }
    public string subjectAamsCode { get; set; }
    public string subjectDenomination { get; set; }
    public string subjectVatNumber { get; set; }
    public string subjectTaxCode { get; set; } }

在angularjs控制器中我有:

 $scope.objSearch = {
        "aAMSID": "",
        "venueName": "",
        "address": "",
        "businessSK": "",
        "subjectAamsCode": "",
        "subjectDenomination": "",
        "subjectVatNumber": "",
        "subjectTaxCode": ""
        }

$scope.searchVenueByParameters = function () {

    var strObjSearch = angular.toJson($scope.objSearch);
    console.log(strObjSearch);
    $http({
        url: '/api/SearchVenueByParameters_Result/',
        method: 'GET',
        params: { obj_search: strObjSearch }
    }).success(function (data) {
        $scope.Venues = data;
        $scope.VenueList = true;
        $scope.showItem = true;

})
   .error(function () {
       $scope.error = "An Error has occured while loading posts!";
   });
    }

但是在这种情况下,我已经修正了angularjs控制器在控制台中打印的值,但是在web abi控制器中传递的对象始终为null。

2 个答案:

答案 0 :(得分:1)

使用GET发送复杂对象时,请使用直接从URL创建对象的[FromUri]属性。

在客户端中,将对象添加到参数

$http({
    url: '/api/SearchVenueByParameters_Result/',
    method: 'GET',
    params: $scope.objSearch
}).success(function (data) {
    $scope.Venues = data;
    $scope.VenueList = true;
    $scope.showItem = true;

网址将翻译为

/api/SearchVenueByParameters_Result/?aAMSID=1&venueName=1&address=1&businessSK=1&subjectAamsCode=1&subjectDenomination=1&subjectVatNumber=1&subjectTaxCode=1

在控制器中,添加[FromUri]属性

[HttpGet]
public IEnumerable<SearchVenueByParameters_Result> Get([FromUri]ObjSearch obj_search)

您可以阅读有关属性here

的更多信息

答案 1 :(得分:0)

您应该使用Post将对象发送到WEB API。用这些

替换你的代码
 $scope.objSearch = {
        "aAMSID": "",
        "venueName": "",
        "address": "",
        "businessSK": "",
        "subjectAamsCode": "",
        "subjectDenomination": "",
        "subjectVatNumber": "",
        "subjectTaxCode": ""
        }

$scope.searchVenueByParameters = function () {

    var strObjSearch = angular.toJson($scope.objSearch);
    console.log(strObjSearch);
    $http({
        url: '/api/SearchVenueByParameters_Result/',
        method: 'POST',
        params: { obj_search: strObjSearch }
    }).success(function (data) {
        $scope.Venues = data;
        $scope.VenueList = true;
        $scope.showItem = true;

})
   .error(function () {
       $scope.error = "An Error has occured while loading posts!";
   });
    }

[HttpPost]
public IEnumerable<SearchVenueByParameters_Result> Get(string parameter)
    {
        ObjSearch obj_search = new JavaScriptSerializer().Deserialize<ObjSearch>(parameter);