Hi @Ali Yılmaz,
If you want to ignore specified property when serializing, You can use [JsonIgnore] under System.Text.Json.Serialization
namespace.
Please refer to this simple demo:
using System.Text.Json.Serialization;
namespace WebApplication1.Model
{
public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
public string Country { get; set; }
}
}
Controller
[HttpGet]
public IActionResult Get()
{
List<TestModel> models = new List<TestModel>();
TestModel model = new TestModel()
{
Id = 1,
Name = "aaaa",
Country = "USA"
};
TestModel model2 = new TestModel()
{
Id = 2,
Name = "bbb",
Country = "CN"
};
models.Add(model);
models.Add(model2);
var json = JsonSerializer.Serialize(models);
return Ok(json);
}
Result
You can find it ignore Country
property successfully.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Xinran Shen