I created a new ASP.NET MVC 4 web application and changed the default ValuesController value:
using System.Runtime.Serialization;
using System.Web.Http;
namespace WebSerializationTest.Controllers
{
public class ValuesController : ApiController
{
public ApiResponse Get()
{
ApiResponse res = new ApiResponse();
res.Length = 120;
return res;
}
}
[DataContract]
public class ApiResponse
{
[DataMember(Name = "length")]
public int Length { get; set; }
}
}
Now, when I make a request from the browser (with setting headers for Accept: application / json), I get this JSON:
[
120
]
However , when I change the name of DataMember to something else, even [DataMember (Name = "Length")]
I get the correct JSON:
{
"Length" : 120
}
Is the string length forbidden or what causes this behavior?
Btw. I have a target environment installed on the .NET Framework 4, but I also tried the .NET Framework 4.5.1 and the problem is still there.
source
share