How to create action filter which will remove null values from Response.Content?
Suppress NULL Values in every API call
Want to suppress null values only for one Method
How to create action filter which will remove null values from Response.Content?
Suppress NULL Values in every API call
Want to suppress null values only for one Method
If you use Formatters.JsonFormatter
in your WebApiConfig.cs
then
1) If you have to ignore null values at application level means for all your api actions
The filter will be
public class IgnoreNullValuesFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
actionExecutedContext.ActionContext.RequestContext.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
base.OnActionExecuted(actionExecutedContext);
}
}
And add this filter to your WebApiConfig.cs
like
config.Filters.Add(new IgnoreNullValuesFilter());
After adding this filter to you will find all your api actions return data with ignoring null values
You don't need to annotate your api action [IgnoreNullValuesFilter]
with this because we added it as globally in WebApiConfig.cs
For point no. 2) and 3) don't add filter in WebApiConfig.cs
2) If you have to ignore null values for specific api actions with action filter
Then filter will be
public class IgnoreNullValuesFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
var type = objectContent.ObjectType;
var value = JsonConvert.SerializeObject(objectContent.Value, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
objectContent.Value = value;
//If you want to send your response as strongly type
JObject jObject = JObject.Parse(value);
objectContent.Value = jObject;
}
base.OnActionExecuted(actionExecutedContext);
}
}
And your api for ignore null values
[HttpGet]
[IgnoreNullValuesFilter]
public IHttpActionResult ApiName()
{
var myObject = new Item { Id = 1, Name = "Matthew", Salary = 25000, Department = null };
return Ok(myObject);
}
If you want to include null values then just remove [IgnoreNullValuesFilter] from your api
3) If you don't want to use any action filter then
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public long Salary { get; set; }
public string Department { get; set; }
}
And your API will be look like
[HttpGet]
public IHttpActionResult ApiName()
{
var myObject = new Item { Id = 1, Name = "Matthew", Salary = 25000, Department = null };
return Json(myObject, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
}
OR
[HttpGet]
public IHttpActionResult ApiName()
{
var myObject = new Item1 { Id = 1, Name = "Matthew", Salary = 25000, Department = null };
var jsonIgnoreNullValues = JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
JObject jObject = JObject.Parse(jsonIgnoreNullValues);
return Ok(jObject);
}
Result:
Here you see in response that Item
has property department = null
ignore by formatter.
You can send myObject
as any type of data that you want