gpt4 book ai didi

c# - 防止在 WebAPI OData v4 服务中发出空值

转载 作者:行者123 更新时间:2023-11-30 20:21:48 27 4
gpt4 key购买 nike

我正在运行 WebAPI OData v4 服务,我不希望发出空值。例如,当我请求一个 User 对象时,将返回以下内容:

"value": [
{
"FirstName": "John",
"LastName": "Smith",
"EmailAddress": "john@smith.com",
"PasswordHash": null,
"PhoneNumber": "11234567890",
"Website": null,
"Id": "dac9706a-8497-404c-bb5a-ca1024cf213b"
}

由于 PasswordHashWebsite 字段为空,我希望它们不包含在输出中。我在应用程序的注册 block 中尝试了以下几行,但它们都不起作用:

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 序列化。

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,如下所示:

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's answer 的代码实现一个 CustomSerializerProvider 类,然后我可以在 Register 中使用它。

关于c# - 防止在 WebAPI OData v4 服务中发出空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com