Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Model:
C#
public class ClientSetupDto
{
        public byte Status { get; set; } = 0;

        public string ClientName { get; set; }

        public string ClientId { get; set; }
}


API Request:
C#
[HttpPost]
 [ProducesResponseType(200)]
 [ProducesResponseType(400)]
 public async Task<ActionResult<BaseCommandResponse>> Post([FromBody] ClientSetupDto entity)
 {
... some code here ...
 }


Accepted API Request - No problem here
{
  "status": 0,
  "clientName": "string",
  "clientId": "string",
}


Not Accepted.
{
  "status": "",
  "clientName": "string",
  "clientId": "string",
}

I do understand the Status property is of byte type in DTO and can not accept string or null values. In this case
ClientSetupDto entity
returns null and API throws a null value exception.

I want to achieve one of below:
1-Disable DTO model validation at API level. I have exception handling at a later stage
2-ClientSetupDto return whatever values passed into it and does not return null.

What I have tried:

In program.cs I implemented below but it does not work:

C#
builder.Services.Configure<ApiBehaviorOptions>(opt => {
    opt.SuppressModelStateInvalidFilter = true;
});
Posted
Comments
Richard Deeming 1-Nov-22 6:33am    
"ClientSetupDto return whatever values passed into it and does not return null"
That's a contradiction - if it returns whatever values are passed into it, then if you pass in null, it must by definition return null.

I'd start by fixing your client calls to pass valid data. If you make a call passing invalid data, it's perfectly reasonable to return an error response - preferably a 400 Bad Request.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900