【发布时间】:2016-08-02 10:48:25
【问题描述】:
我调用了返回 504 的 ApiController,但我无法弄清楚我做错了什么......
谁能看到我的问题?
这是控制器:
public class SA_GetAllLocationsController : ApiController
{
[Route("SA_GetAllLocations")]
[HttpGet]
public List<SA_Locations> Get()
{
List<SA_Locations> result = SADatastore.Functions.Location.GetAllLocations(Settings.Default.ConnectionString);
return result;
}
}
这是由 Entity Framework Database First 生成的 SA_Locations 类...
public partial class SA_Locations
{
public SA_Locations()
{
this.SA_Items = new HashSet<SA_Items>();
}
public string LocationID { get; set; }
public string BuildingID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MISID { get; set; }
public string GPSCoords { get; set; }
public string QRCode { get; set; }
public Nullable<bool> Signable { get; set; }
public Nullable<bool> Auditable { get; set; }
public Nullable<bool> Bookable { get; set; }
public Nullable<bool> Entrance { get; set; }
public Nullable<bool> Exit { get; set; }
public Nullable<bool> Active { get; set; }
public System.DateTime LastUpdated { get; set; }
public virtual SA_Buildings SA_Buildings { get; set; }
public virtual ICollection<SA_Items> SA_Items { get; set; }
}
我写了一个完美的“状态”函数,看起来像这样:
public HttpResponseMessage Get()
{
return new HttpResponseMessage()
{
Content = new StringContent(
"SA service is running and ready to accept connections!",
Encoding.UTF8,
"text/html"
)
};
}
我无法弄清楚为什么在我调试时 SA_Locations 似乎没有错误地返回,但我在 Fiddler 中看到的响应是:
504 - 服务器没有为此请求返回完整的响应。服务器返回 0 个字节
对象是否过于复杂而无法返回?
任何帮助将不胜感激!
特雷夫
更新
我决定尝试看看如果我只从控制器返回纯 JSON 会发生什么,所以我重新编写了控制器,如下所示:
public class SA_GetAllLocationsController : ApiController
{
[Route("SA_GetAllLocations")]
[HttpGet]
public HttpResponseMessage Get()
{
List<SA_Locations> result = SADatastore.Functions.Location.GetAllLocations(Settings.Default.ConnectionString);
string output = JsonConvert.SerializeObject(result);
return new HttpResponseMessage()
{
Content = new StringContent(
output,
Encoding.UTF8,
"application/json"
)
};
}
}
现在将 SA_Locations 对象序列化为字符串时实际上会引发错误!
错误信息是:
Newtonsoft.Json.dll 中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理
附加信息:从“System.Data.Entity.DynamicProxies.SA_Locations_720FF8784B152496318F87BCB674E344B97C0446B16D4DC2BDDC381D88C048B9”上的“SA_Buildings”获取值时出错。
所以,它确实指向了与 SA_Locations 相关的东西,正如我所说,它是由实体框架数据库首先生成的一个类....
越来越近,但不知道为什么该对象不会序列化?
【问题讨论】: