Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JsonIgnore Not Work With Swagger When Using NewtonSoft

net core 5, I stopped using System.text.Json for some reason and I use NewtonSoft and I changed all the namespaces, for example, to JsonIgnore, but my problem is that the properties that I ignored are still displayed in the swagger output.

 using System;
 using Newtonsoft.Json;
 public class AddEmployeeDto
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [JsonIgnore]
    public int EmployeeType { get; set; }
}

this is swagger output :

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since Sytem.text.Json has become the default Serialize tool of Asp.Net Core, the standard version of Swagger is also compatible with it,
and to change it, like this change that you have probably applied,
services.AddControllers().AddNewtonsoftJson();

You must install the package From Nuget:

Swashbuckle.AspNetCore.Newtonsoft

And register it in your Service Container as follows to solve your problem

services.AddSwaggerGenNewtonsoftSupport();

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading