如何从webapi httppost控制器moethod返回一个对象

时间:2017-09-11 12:41:31

标签: c# angularjs asp.net-web-api

我正在尝试从WebAPI HttpPost控制器方法返回一个对象,但该对象没有回到我的AngularJS服务,尽管帖子成功了。正在调用控制器方法,因为我可以在我们的数据库中看到它的结果。

使用以下代码

调用WebAPI方法
erm.service('ERM_ERWeb_MobileSync_Service', ['$http', '$rootScope', 'ERM_Common_Factory', 'ERM_Common_Service',
function ($http, $rootScope, ERM_Common_Factory, ERM_Common_Service) {
    this.SendData = function (payload) {
        console.log('ERM_ERWeb_MobileSync_Service.SendData called...');

        $http.defaults.headers.common.Authorization = 'Basic ' + ERM_Common_Service.Base64Encode(ERM_Common_Factory.GenAuthString());

        $http({
            url: ERM_Common_Factory.GetWebAPILocation() + '/API/MobileSync/ConsumeData',
            dataType: 'json',
            method: 'POST',
            data: payload,
            headers: {
                "Content-Type": "application/json"
            }
        })
        .then(function (response) {
            $rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Complete', {});
        },
        function (error) {
            $rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Error', {});
        });
    };
}]);

这是控制器方法。

[HttpPost]
[Route("ConsumeData")]
[ERMobileInstanceAuth]
public IHttpActionResult ConsumeData([FromBody]MobileSyncPayload msp)
{
    try
    {
        _individualService.SetNoShows(msp.NoShows.Select(i => (Models.Data.Instance.T_Individual)i).ToList());
        _flightService.SetFlightTimes(msp.Flights.Select(f => (Models.Data.Instance.T_FlightLog)f).ToList());

        Dictionary<Guid, int> vNums = new Dictionary<Guid, int>();

        foreach (Voucher v in msp.Vouchers)
        {
            Dictionary<Guid, int> vNumsTemp = _voucherService.SyncVouchers(new List<Models.Data.Instance.T_Voucher>() { v });

            vNums = vNums.Concat(vNumsTemp).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            _voucherLineService.SyncVoucherLines(v.VoucherGUID, v.VoucherLines.Select(vl => (Models.Data.Instance.T_VoucherLine)vl).ToList());
            _cashPaymentService.SyncCashPayments(v.CashPayments.Select(cp => (Models.Data.Instance.T_CashPayment)cp).ToList());
            _cardPaymentService.SyncCardPayments(v.CardPayments.Select(cp => (Models.Data.Instance.T_CardPayment)cp).ToList(), true);

            List<CardPaymentAttempt> acpa = new List<CardPaymentAttempt>();

            foreach (CardPayment cp in v.CardPayments)
            {
                acpa.AddRange(cp.CardPaymentAttempts);
            }

            _cardPaymentAttemptService.SyncCardPaymentAttempts(acpa.Select(cpa => (Models.Data.Instance.T_CardPaymentAttempt)cpa).ToList());
        }

        var returnobject = new { Response = ConvertToList(vNums) };

        return Ok(returnobject);
    }
    catch (Exception e)
    {
        Logging.CreateLogEntry("DataService", Logging.ExceptionHandler.Format(e, Logging.ExceptionHandler.LogLevel.Exception));

        return InternalServerError();
    }
}

我必须有一些简单的东西,但是我看不出它是什么。有人有任何想法吗?

1 个答案:

答案 0 :(得分:0)

如果我在emit

中返回post数据对象,则会有帮助
$rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Complete', { Response: response.data.Response });