Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set NullValueHandling at a controller level

For the moment part, i would like to exclude null values from my api response, so in my startup.cs file, i have this.

services.AddMvc()
    .AddJsonOptions(options =>
    {
        // Setup json serializer
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    });

But is it possible to state that on 1 or more controllers, i actually want to include NULL values??

like image 364
Gillardo Avatar asked Feb 04 '16 16:02

Gillardo


1 Answers

One option is to create custom Json result type, as described in this question: Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?. Then you can have bool var on base controller and use it do disable null's when using custom Json result or even pass option directly:

return Json(data, ignoreNulls: true); 
like image 168
csharpfolk Avatar answered Oct 03 '22 17:10

csharpfolk