Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core: controlling Json generated by Controller.Json method

By default, Controller.Json generates JSON for each public member of a class. How can I change this so that some members are ignored. Please note that I am using .Net Core.

Example:

[HttpGet("/api/episodes")]
public IActionResult GetEpisodes()
{
  var episodes = _podcastProvider.Get();

  return Json(episodes);
}

Thanks.

like image 961
Adrian S Avatar asked Apr 23 '26 18:04

Adrian S


1 Answers

You can use [JsonIgnore] attribute that available in Newtonsoft.Json namespace like below:

public class Model
{
   public int Id { get; set; }
   public string Name { get; set; }
   [JsonIgnore]
   public int Age { get; set; }
}
like image 150
Ali Avatar answered Apr 25 '26 06:04

Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!