I am working with web api 2 which I coincidentally realized that it rounds the long
type value in the response model if it is longer than 16 digits.
(simplifying the example for the sake of the question) Assume I have a web api method as "GetOrders" which returns list of orderVm model which is defined as below:
public class OrderVm{
public int OrderID {get;set;}
public long? OrderNumber {get;set;}
}
What is happening is, if my order number value is
1234567890123459
there is no issue - total of 16 digits.1234567890123499
there is no issue - total of 16 digits.12345678901234599
this is the problematic one. total of 17 digits. It is rounding this to12345678901234600
Below is my simplified example API Method:
[HttpPost]
[Route("get-orders/")]
public IHttpActionResult GetOrders(PostedVm postedVm)
{
var orderDtoList = _orderManager.GetOrders(postedVm.Active); // I checked the value in orderDtoList and it is not rounded at this moment...
var orderVmList = MapDtoToVm(orderDtoList);
return Ok(orderVmList);// I thought maybe something happens during the property mapping (which is just using simple reflection) but I checked the value in orderVmList and it is not rounded at this moment...
}
Even though right before returning the response, I checked that the values actually were not rounded.. But somehow on the client, it is coming as rounded. I thought maybe the browser does some magic (for some reason) but I checked my payload on postman and fiddler, it was showing that the value as rounded in the response payload.
This sounds something related to web api configuration but if so, how I can set it to not modify the data? And I am also curious about "why it is happening after 16 digits?"
Just in case I added my WebApiConfig class below:
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
//GZip Compression
GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Routes.MapHttpRoute(
name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {
id = RouteParameter.Optional
});
}
}