Home > Software design >  How to set Swagger ignore id property on Create method? C# ASP.NET Core Swagger
How to set Swagger ignore id property on Create method? C# ASP.NET Core Swagger

Time:07-09

How can I ask Swagger not to ask id for POST method (creation of entity in DB)?

{
  "id": 0,
  "name": "string",
  "quantity": 0,
  "typeId": 0,
  "brandId": 0
}

There is an auto-incrementation in my SQL Server, so I think on frontend it's not necessary to input Id and there will be no field for Id. But is it possible to ask swagger not to show id?

CodePudding user response:

Create a dedicated create dto:

public class CreateDto {

  public class Name { get; set;}
  public int Quantity { get; set;}
  ...
}

and in your controller create an instance of your db entity from the incoming CreateDto object.

CodePudding user response:

You could try with JsonIgnore attribute if you don't need to Serialize the model somewhere else in your project I tried as below:

public class TestModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [JsonIgnore]
        public string Hide { get; set; }
    }

The result: enter image description here

  • Related