小编Emi*_*sen的帖子

新的ASP.NET Core API返回空的JSON对象

我已经制作了一个.NET Core Web API项目来测试它.

我的问题是,当我请求端点时,API返回一个空的JSON对象,例如位于"/ api/cars/123".无论我放入什么类型的对象,都会发生这种情况,除非它是任何原始数据类型或其数组.响应总是:

{}
Run Code Online (Sandbox Code Playgroud)

在全新的Visual Studio 2017安装中,应用程序的配置完全是默认的.

我有以下课程:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net

6
推荐指数
1
解决办法
2489
查看次数

标签 统计

asp.net ×1

c# ×1