【发布时间】:2018-06-07 13:55:05
【问题描述】:
我制作了一个非常简单的 Web API 控制器来在更复杂的控制器中重现错误,这个控制器只有最少量的代码来重现错误。
控制器代码如下
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace NWCloudBorgEmployee.Controllers
{
public class WmsInfo
{
public string StoreName { get; set; }
public string ItemName { get; set; }
public string ItemQty { get; set; }
}
public class WmsInformation
{
public List<WmsInfo> WmsInfos { get; set; }
}
[Produces("application/json")]
[Route("api/WmsInfo")]
public class WmsInfoController : Controller
{
// GET: api/WmsInfo
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "You need to send in a barcode as ID to get the correct return data" };
}
// GET: api/WmsInfo/5
[HttpGet("{id}", Name = "Get")]
public string Get(int id)
{
var test = new WmsInformation();
test.WmsInfos = new List<WmsInfo>
{
new WmsInfo { StoreName = "SE001", ItemName = "Item1" , ItemQty = "10"},
new WmsInfo { StoreName = "SE002", ItemName = "Item2" , ItemQty = "115"}
};
return test.ToString();
}
}
}
当我调用 API 时,我得到如下字符串而不是 JSON 数据
"NWCloudBorgEmployee.Controllers.WmsInformation"
为什么不返回 JSON?
【问题讨论】:
-
test.ToString()是默认的吗?如果它是默认值,您不应该覆盖它吗?
标签: c# json asp.net-core-webapi