我想從angularJs控制器傳遞對象參數到Web Api 2獲取方法控制器。AngularJS傳遞參數到Web Api獲取
如果我傳遞對象像字符串它將工作,但我不喜歡這個解決方案。
[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!";
});
}
但在這種情況下,我已更正值打印ed在控制檯從angularjs控制器,但傳遞在網絡abi控制器的對象始終爲空。
我也可以使用FromBody嗎?因爲它不起作用 –
@FrancescoGaleota GET請求是設計沒有一個正文。所以[FromBody]不適用於GET。 –