I want to send a post request (using postman) to Asp.net core web API and my API is like this:
[HttpPost]
public IActionResult Create(CreateTransferItemCommand command)
{
try
{
_transferCommandFacade.CreateTransferItem(command);
return Ok(command);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
And my CreateTransferItemCommand model is like this:
public class CreateTransferItemCommand : Command
{
public Guid TransferId { get; set; }
public int AssetNumber { get; set; }
public int PartId { get; set; }
public int EmployeeId { get; set; }
public string Description { get; set; }
public long StuffDocId { get; set; }
}
I have below code in startup.cs and ConfigureServices method:
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
And I set request body in postman like this:
{
"transferId":"fd3f3cd5-f52f-4b8e-b479-7bfe6dc6dc5f",
"assetNumber":"",
"partId":"",
"employeeId":"",
"description":"",
"stuffDocId":""
}
The problem is that I can set "int" properties to empty string but I can not set long property (stuffDocId) to empty string! If I set stuffDocId to empty string postman responses like this:
{
"errors": {
"stuffDocId": [
"Error converting value \"\" to type 'System.Int64'. Path 'stuffDocId', line 1, position 179."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLOEDQM8HLQS:0000000A"
}
If I set stuffDocId to null it's working. But I want to set it to empty string. How can I solve this problem?! And why int and long don't have same behavior in this situation?