I have a class Company that looks like this.
public class Company
{
[JsonPropertyName("companyID")]
public string CompanyID { get; set; }
[JsonPropertyName("vehicleIDs")]
public List<string> VehicleIDs { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("companyName")]
public string CompanyName { get; set; }
}
This is my first time making an API, also first time working with C#. I am creating a List and trying to return it as a Json from my GET method. However, anyway that I have tried to do so seems to result in a ton of information I have no clue where it’s coming from being there. I have also tried formatting so that it appears indented and this also does not seem to be working.
Currently my GET method is just returning a List, and in my WebApiConfig.cs Register method I have
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
This is working to a degree, and i’m at least able to use and access my list of objects I receive, but the Json looks like this
[{"$id":"1","CompanyID":"28","VehicleIDs":["1900","2121","1802","2125","1701","1703","1702"],"Email":"ElizabethHarper","CompanyName":"Allegany Paratransit","Configuration":null,"ControllerContext":{"$id":"2","Configuration":null,"ControllerDescriptor":null,"Controller":null,"Request":null,"RequestContext":{"$id":"3","Request":null,"ClientCertificate":null,"Configuration":null,"IncludeErrorDetail":false,"IsLocal":false,"RouteData":null,"Url":null,"VirtualPathRoot":null,"Principal":{"$id":"4","m_identity":{"$id":"5","System.Security.ClaimsIdentity.version":"1.0","System.Security.ClaimsIdentity.nameClaimType":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name","System.Security.ClaimsIdentity.roleClaimType":"http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid","System.Security.ClaimsIdentity.claims":"AAEAAAD/////AQAAAAAAAAAEAQAAAI4BU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuTGlzdGAxW1tTeXN0ZW0uU2VjdXJpdHkuQ2xhaW1zLkNsYWltLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkz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THpJd01EVXZNRFV2YVdSbGJuUnBkSGt2WTJ4aGFXMXpMMjVoYldVR0JBQUFBRUJvZEhSd09pOHZjMk5vWlcxaGN5NXRhV055YjNOdlpuUXVZMjl0TDNkekx6SXdNRGd2TURZdmFXUmxiblJwZEhrdlkyeGhhVzF6TDJkeWIzVndjMmxrQmdVQUFBQ0FBMEZCUlVGQlFVUXZMeTh2TDBGUlFVRkJRVUZCUVVGQlJVRlJRVUZCU1RSQ1ZUTnNlbVJIVm5STWEwNTJZa2Q0YkZrelVuQmlNalY2VEd0a2JHSnRWbmxoVjAxMVZFZHNlbVJIUVhoWE1YUlVaVmhPTUZwWE1IVlZNbFpxWkZoS2NHUklhM1ZSTW5ob1lWY3hla3hyVG5OWlYyeDBURU5DZEdNeVRuWmpiWGh3V1dsM1oxWnRWbmxqTW14Mlltb3dNRXhxUVhWTlF6UjNURU5DUkdSWGVEQmtXRXBzVUZjMWJHUllVbmxaVjNkelNVWkNNVmx0ZUhCWk1IUnNaVlpTZG1FeVZuVlFWMGt6VGpKRk1WbDZWVEpOVkd0NlRrZFZkMDlFYkdSWVVVMUJRVUZCUjFneWJEQmFWekY2UWxZNWVtRlljR3hEUmpreVdsaEtlbUZYT1hWQmQwRkJTR3hPTldNelVteGlVelZVV2xkT01XTnRiREJsVXpWRVlrZEdjR0pZVFhWUk1uaG9ZVmN4WWxoUlowbERVVWxCUVVGQlFVRkJRVUZCUVVGQlFVRmpRMEZCUVVGQlFVVkJRVUZCUVVGQlFVRkJlSGhVWlZoT01GcFhNSFZWTWxacVpGaEtjR1JJYTNWUk1uaG9ZVmN4ZWt4clRuTlpWMngwUTNjOVBRcz0NAgs="}},"RouteData":null},"ActionContext":{"$id":"7","ControllerContext":{"$ref":"2"},"ActionDescriptor":null,"ModelState":{},"ActionArguments":{},"Response":null,"Request":null,"RequestContext":{"$ref":"3"}},"ModelState":{},"Request":null,"RequestContext":{"$ref":"3"},"Url":null,"User":{"$ref":"4"}},{"$id":"8","CompanyID":"27","VehicleIDs":["312","311","2105"],"Email":"[email protected]","CompanyName":"Allegany Fixed Bus","Configuration":null,"ControllerContext":{"$id":"9","Configuration":null,"ControllerDescriptor":null,"Controller":null,"Request":null,"RequestContext":{"$id":"10","Request":null,"ClientCertificate":null,"Configuration":null,"IncludeErrorDetail":false,"IsLocal":false,"RouteData":null,"Url":null,"VirtualPathRoot":null,"Principal":{"$ref":"4"}},"RouteData":null},"ActionContext":{"$id":"11","ControllerContext":{"$ref":"9"},"ActionDescriptor":null,"ModelState":{},"ActionArguments":{},"Response":null,"Request":null,"RequestContext":{"$ref":"10"}},"ModelState":{},"Request":null,"RequestContext":{"$ref":"10"},"Url":null,"User":{"$ref":"4"}}]
I have no idea where or what this extra text is coming from. I have tried returning it as a Json string using both Newtonsoft.Json and System.Text.Json, both seem to produce similar results.
Can anyone explain?
3
Answers
I discovered the issue. I'm not quite sure when this happened, may have been the result of intellisense, I don't know.
This:
Became:
Removing : ApiController has resolved the issue.
you can Try to to provide return type as json
Like we have actionresult Task just reomve and make method to return Json.
like public jsonresult
It appears as though it is returning some sort of httpresponse within each Company entity. I don’t know how you managed to do this; I haven’t seen anything like that before. I notice that the attributes seem to have no effect — the property names in the returned json are Pascal case rather than the casing you assigned in JsonPropertyName.
I have a different approach that has always worked for me where the attributed json property names will be converted.
If you are willing to use Newtonsoft.Json, attribute the properties in your Company entity with Newtonsoft’s JsonProperty instead of JsonPropertyName, and you can convert each Company object to a JObject and add them all to a JArray before returning them in a response. Something like this in your controller: