从api响应中动态删除属性

Tus*_*arJ 0 c# api asp.net-web-api azure-api-apps

我有一个api,其响应如下
"prop1":"SomeValu1",
"prop2":"SomeValue2" , "prop3
":null,
"prop4":"SomeValue4"

问题是,基于输入,一些属性将为null(预期行为),我不希望在响应中返回.像这样的东西(prop3不存在)

"prop1":"SomeValu1",
"prop2":"SomeValue2",
"prop4":"SomeValue4"

哪个属性为null基于运行时逻辑.任何想法我怎么能这样做?

Aas*_*mar 6

如果您正在使用JSON,那么您可以尝试这样做:

JsonConvert.SerializeObject(yourObject, 
                        Newtonsoft.Json.Formatting.None, 
                        new JsonSerializerSettings { 
                            NullValueHandling = NullValueHandling.Ignore
                        });
Run Code Online (Sandbox Code Playgroud)