29

I have made a .NET Core Web API project to test it out.

My problem is, that the API returns an empty JSON object, when I request the endpoint, which is located at "/api/cars/123" for instance. This happens no matter what kind of object I put in, unless it's any primitive datatype, or an array thereof. The response is always:

{}

The configuration of the application is completely default, on a fresh Visual Studio 2017 installation.

I have the following classes:

Car.cs

namespace Ex6.Entities
{
    public class Car
    {
        private int Id { get; set; }
        private string Make { get; set; }
        private string Model { get; set; }

        public Car(int Id, string Make, string Model)
        {
            this.Id = Id;
            this.Make = Make;
            this.Model = Model;
        }
    }
}

CarsController.cs:

using Microsoft.AspNetCore.Mvc;
using Ex6.Entities;

namespace Ex6.Controllers
{
    [Route("api/[controller]")]
    public class CarsController : Controller
    {

        [HttpGet("{id}")]
        public JsonResult GetCar(int id)
        {
            return Json(new Car(1, "Toyota", "Aygo" ));
        }

    }
}

Am I missing something?

6
  • Yeah, updating the question again. It made no difference though :S Commented Jun 1, 2017 at 23:17
  • Yes, and if I try mapping the Car object to a variable before returning it, I can see in the debugger that it's constructed properly. :) Commented Jun 1, 2017 at 23:20
  • Adding the autogenerated files now Commented Jun 1, 2017 at 23:22
  • I access it through the API, by sending a GET request to "/api/cars/123" for example. Commented Jun 1, 2017 at 23:28
  • Do i need a JSON serializer, or will ASP.NET core take care of it automatically? Commented Jun 1, 2017 at 23:29

3 Answers 3

59

In order for the JsonSerializer to be able to see and serialize your properties, they need to be public:

public int Id { get; private set; } //the setters can be private
public string Make { get; set; }
public string Model { get; set; }
Sign up to request clarification or add additional context in comments.

5 Comments

I encountered the same problem when converting a legacy WCF to WebAPI. However, the problem I had was a result class that had all fields and no properties. I added a getter and setter to all the fields to convert them to auto-properties everything worked.
Couldn't find this in documentation. Wish it was easier to find this answer...
Can JsonSerializer be configured to report these kind of issues?
Not the issue with the OP, but it's worth noting that they also can't be fields, even if public.
Incredible. Coming from Newtonsoft where fields worked, this had me stumped until I found this answer.
4

For some reason, Text.JSON doesn't serialize public fields as well, so they need to be properties. This solved the similar issue for me

Comments

0

Try this by adding configure

services.AddControllersWithViews().AddNewtonsoftJson();

....AddNewtonsoftJson() this should solve the issue.

1 Comment

Not sure why this was downvoted. This fixed it immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.