.Net核心Web API未从angularJS反序列化JSON

.Net核心Web API未从angularJS反序列化JSON,angularjs,asp.net-core,asp.net-web-api,Angularjs,Asp.net Core,Asp.net Web Api,我有一个angular JS控制器,我将视图模型序列化为json,而json不会在后端使用web api进行反序列化 这是我的角度控制器构造函数 constructor($scope, $http, $routeParams: IBookingParams) { this.http = $http; //get parameters from Recommendation page this.bookingView = <IBooking

我有一个angular JS控制器,我将视图模型序列化为json,而json不会在后端使用web api进行反序列化

这是我的角度控制器构造函数

constructor($scope, $http, $routeParams: IBookingParams) {

        this.http = $http;

        //get parameters from Recommendation page
        this.bookingView = <IBookingViewModel>{};
        this.bookingView.CampaignName = $routeParams.CampaignName;
        this.bookingView.CampaignSupplierId = $routeParams.CampaignSupplierId;
        this.bookingView.SupplierName = $routeParams.SupplierName;
        this.bookingView.MediaChannelNames = $routeParams.MediaChannelNames;
        this.bookingView.MediaChannelIds = $routeParams.MediaChannelIds;                    

        let livedate = this.GetJSDate($routeParams.LiveDate);
        let liveDateTime = this.GetDateTime(livedate);

        this.bookingView.LiveDate = liveDateTime;
        //populate the rest of our model 
        this.bookingView.Action = "from angular";

        var model = this.bookingView;
        let json = JSON.stringify(model);               

        this.http({
            url: "/api/asdabooking",
            method: "POST",
            data: json
        })
            .then((response: any) => {
                let test = "";
            })
            .catch((data: any) => {
                let test = "";
            });
    }
构造函数($scope、$http、$routeParams:IBookingParams){
this.http=$http;
//从推荐页面获取参数
this.bookingView={};
this.bookingView.campaigname=$routeParams.campaigname;
this.bookingView.ActivitySupplierId=$routeParams.ActivitySupplierId;
this.bookingView.SupplierName=$routeParams.SupplierName;
this.bookingView.MediaChannelNames=$routeParams.MediaChannelNames;
this.bookingView.MediaChannelId=$routeParams.MediaChannelId;
让livedate=this.GetJSDate($routeParams.livedate);
让liveDateTime=this.GetDateTime(livedate);
this.bookingView.LiveDate=liveDateTime;
//填充模型的其余部分
this.bookingView.Action=“从角度”;
var模型=this.bookingView;
让json=json.stringify(model);
这是http({
url:“/api/asdabooking”,
方法:“张贴”,
数据:json
})
。然后((响应:任意)=>{
让测试=”;
})
.catch((数据:任意)=>{
让测试=”;
});
}
这是我的web api

[HttpPost]
    [Route("api/asdabooking")]
    public async Task<IActionResult> BuildBookingModel([FromBody]BookingViewModel model)
    {
        try
        {
            //model is null??!!
            return Ok("");
        }
        catch (Exception ex)
        {
            base.Logger.LogError(ex.Message, ex);
            return BadRequest(ex.Message);
        }
    }
[HttpPost]
[路线(“api/asdabooking”)]
公共异步任务BuildBookingModel([FromBody]BookingViewModel模型)
{
尝试
{
//模型为空??!!
返回Ok(“”);
}
捕获(例外情况除外)
{
base.Logger.LogError(例如消息,例如);
返回请求(例如消息);
}
}
这很奇怪,前端的bookingView视图模型与后端视图模型“BookingViewModel”上的字段相匹配。我已经检查了json,看起来一切正常

这是我的视图模型

public class BookingViewModel
{
    public string CampaignName { get; set; }
    public string CampaignSupplierId { get; set; }
    public string SupplierName { get; set; }
    public List<string> MediaIds { get; set; }
    public List<string> MediaChannelNames { get; set; }
    public List<MediaChannelViewModel> MediaChannels { get; set; }
    public string Action { get; set; }
    public DateTime LiveDate { get; set; }
    public List<int> MediaChannelIds { get; set; }
    public int SupplierId { get; set; }     
    public bool SuccessfulSave { get; set; }
    /// <summary>
    /// Track which tab is updating
    /// </summary>
    public string TabAction { get; set; }
    /// <summary>
    /// Price summary - list of media channels (tabs)
    /// </summary>
    public List<MediaSummaryViewModel> MediaSummaries { get; set; }

    public string UserMessage { get; set; }

}
公共类BookingViewModel
{
公共字符串名称{get;set;}
公共字符串活动供应商ID{get;set;}
公共字符串供应商名称{get;set;}
公共列表媒体ID{get;set;}
公共列表MediaChannelNames{get;set;}
公共列表媒体频道{get;set;}
公共字符串操作{get;set;}
public DateTime LiveDate{get;set;}
公共列表MediaChannelId{get;set;}
public int SupplierId{get;set;}
public bool成功保存{get;set;}
/// 
///跟踪正在更新的选项卡
/// 
公共字符串TabAction{get;set;}
/// 
///价格汇总-媒体频道列表(选项卡)
/// 
公共列表媒体摘要{get;set;}
公共字符串UserMessage{get;set;}
}
这是我的json


当我遇到这个问题时,通常是因为JSON对象中的类型与您在模型中定义的属性类型不匹配。我会确保这些类型匹配。这也可能有助于有兴趣回答这个问题的人发布JSON对象以及模型类的片段。

无节动物应该是

"mediaChannelIds":[ 
  4,
  5]
这是因为我通过多次引用同一参数从使用$routeParams的查询字符串中获取数组,这是一个坏主意。最好用字符分隔值以获取数组,因为使用$routeParams无法使其类型安全。它将始终为您提供字符串


在JSON中,您可以漏掉字段或传递null没有问题,它仍然会反序列化,但您不能不匹配类型,或者整个事件返回为null。

请求中传递了哪些标头?没有,只有请求数据中的JSON。请求中必须传递标头。我怀疑它缺少内容-类型头,应该设置为“application/json”。尝试在开发人员工具或Fiddler之类的工具中查看请求。我以前没有这样做,但我添加了“Content Type”:“application/json”“在http对象的headers参数中,但仍然存在相同的问题。。我会试试postman..当我遇到这样的问题时,通常我会发现通过某种类型的调试工具(如postman)来处理请求是最容易的,然后根据你的发现调整你的代码。这更多的是一个注释而不是一个答案,但我已经添加了这两个。我本来会注释的,但我没有足够的堆栈溢出代表来这么做。看起来您的模型与JSON对象不匹配。首先,JSON属性的名称需要与API模型的名称匹配。如果您纠正了JSON对象中属性的命名,但仍然存在问题,那么您应该看看如何在API端注册该操作。看起来您没有在post请求的正文中提供BookingViewModel的全部内容。要解决这个问题,您可以将控制器的某些属性定义为可选属性。问题是MediaChannelId数组是以带引号的字符串数组的形式出现的。。不过我确实在typescript中指定了一个数字数组。。当它被分配给模型时,有些事情变得很有趣。但是对于记录来说,字段是否完全丢失或赋值为null并不重要,它仍然反序列化。无论如何,谢谢。使用postman和修补json是弄清这些问题的最好方法。啊,这很有趣!我不知道反序列化的行为会将丢失的属性添加到对象中。很高兴你查到了真相