问题描述
我正在运行WebAPI OData v4服务,并且我不想发出空值.例如,当我请求一个User
对象时,将返回以下内容:
I'm running an WebAPI OData v4 service and I don't want null values to be emitted. For example, when I request a User
object, the following is returned:
"value": [
{
"FirstName": "John",
"LastName": "Smith",
"EmailAddress": "[email protected]",
"PasswordHash": null,
"PhoneNumber": "11234567890",
"Website": null,
"Id": "dac9706a-8497-404c-bb5a-ca1024cf213b"
}
由于PasswordHash
和Website
字段为空,我希望它们不包含在输出中.我已经在应用程序的Register块中尝试了以下几行,但是它们都不起作用:
Since the PasswordHash
and Website
fields are null, I'd like them to not be included in the output. I've tried the following lines in the application's Register block, but none of them work:
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
下面是Register方法的全部内容.我正在使用Newtonsoft Json包来处理JSON序列化.
Below is the full contents of the Register method. I'm using the Newtonsoft Json package to handle the JSON serialization.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "UploadComplete",
routeTemplate: "UploadFiles/Complete",
defaults: new { controller = "UploadFiles", action = "Complete" }
);
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Address>("Addresses");
builder.EntitySet<Assignment>("Assignments");
builder.EntitySet<Course>("Courses");
builder.EntitySet<File>("Files");
builder.EntitySet<UploadFile>("UploadFiles");
builder.EntitySet<Organization>("Organizations");
builder.EntitySet<Submission>("Submissions");
builder.EntitySet<Term>("Terms");
builder.EntitySet<User>("Users");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
// Neither of these lines work.
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}
推荐答案
感谢Brian Rogers向我指出了正确的方向,我最终像这样压倒了ODataEntityTypeSerializer
:
Thanks Brian Rogers for pointing me in the right direction, I ended up overriding ODataEntityTypeSerializer
like so:
public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
{
ODataEntry entry = base.CreateEntry(selectExpandNode, entityInstanceContext);
// Remove any properties which are null.
List<ODataProperty> properties = new List<ODataProperty>();
foreach (ODataProperty property in entry.Properties)
{
if (property.Value != null)
{
properties.Add(property);
}
}
entry.Properties = properties;
return entry;
}
然后我使用了 RaghuRam-Nadiminti的答案中的代码实施CustomSerializerProvider
类,然后可以在Register
中使用.
And then I used code from RaghuRam-Nadiminti's answer to implement a CustomSerializerProvider
class which I could then use in Register
.
这篇关于防止在WebAPI OData v4服务中发出空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!