Why does Web API parse a comma-separated int array in an action path correctly but cannot parse a comma-separated decimal array?

I have a Web API project written in .NET Core 2.1. I'm trying to create an action that can take an array of decimal values as a parameter.

If I define the parameter data type as string[] or int[] then when I call the action, passing in a comma-separated string, Web API is able to parse that into an array of strings or an array of ints.

However, if I change the data type of the parameter to decimal[] and call it again with the same comma-separated string, Web API strips out the commas and concatenates the values together to form a single large decimal.

Why does it split a comma-separated string correctly if the parameter is defined as an int[] but not if it's a decimal[]?

Example:

Here's my action:

[HttpGet("details/{shipmentDetailIds}")]
public async Task<List<ShipmentDetail>> GetByShipmentDetailIds(decimal[] shipmentDetailIds)
{
    ...
}

Here's what I'm passing in as an argument when I call it via Postman: Values passed in when calling via Postman

Here's how the array is parsed in the action when I define the parameter as a decimal array:

How the argument is parsed as a decimal array

You can see it effectively stripped out the commas and concatenated the three numbers together into a single large decimal value.

But if I change the data type of the parameter to int[] and call it again from Postman with the same argument values, this time Web API parses the argument correctly, splitting it into three numbers:

How the same argument is parsed as an int array

Why the different behaviour when parsing the same argument as a decimal array and an int array? And how can I get it to parse the comma-separated decimals correctly?

EDIT: I know I could get this to work by passing the array in the query string. However, I'm updating an existing app where all the arguments are passed in the URL path, rather than the query string, so I'd like to continue to use the same convention. Another alternative would be to use an integer array, since that works. However, although the shipmentDetailIds currently appear to be integers the data type in the warehouse management system is decimal. I would prefer to stick to the correct data type in case shipments start coming through with decimal-style shipmentDetailIds in future.



from Recent Questions - Stack Overflow https://ift.tt/2XWsrX6
https://ift.tt/2XTqfQa

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation