的WebAPI犯规序列化空字段 [英] WebApi doesnt serialize null fields

查看:97
本文介绍了的WebAPI犯规序列化空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下按照我的Web API命令:

I've got the following following command in my web api:

return Request.CreateResponse(HttpStatusCode.OK, 
    MyDBContext.DB.Database.SqlQuery<MyCustomerClass>("SELECT * FROM CUSTOMER").ToList());

下面是表:

CREATE TABLE [dbo].[Customer] (
    [CustomerID] [int] NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [LastName] [nvarchar](50) NULL,
    CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
([CustomerID] ASC) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE = OFF, 
      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
      ON [PRIMARY]
) ON [PRIMARY]

GO

我发现,从请求的WebAPI数据时,如果该字段是然后返回JSON结果不包括在返回结果那场。这是预期的行为?

I have found that when requesting data from the webApi, if the fields are null then the returning JSON result doesn't include that field in the return result. Is this expected behaviour?

推荐答案

我发现修复是指定的JSON格式串设置如下:

I found the fix was to specify the following in the json formatter serializer settings:

jsonFormatter.SerializerSettings = new JsonSerializerSettings() 
{ 
   NullValueHandling = NullValueHandling.Include 
};

这篇关于的WebAPI犯规序列化空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆