ApiController返回504-服务器没有返回对此请求的完整响应.服务器返回了0个字节

本文关键字:返回 服务器 响应 0个 字节 504- ApiController 请求 | 更新日期: 2023-09-27 17:59:47

我有一个对ApiController的调用,它返回了一个504,我就是不知道我做错了什么。。。

有人能看到我的问题吗?

这是控制器:

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;
    }
}

这是实体框架数据库首先生成的类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字节

对象是否过于复杂而无法返回?

任何帮助都将不胜感激!

Trev

更新

我决定试着看看如果我只是从控制器返回纯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。Newtonsoft中出现JsonSerializationException。Json.dll,但未在用户代码中处理

附加信息:从"系统"上的"SA_Buildings"获取值时出错。数据实体DynamicProxies。SA_位置_720FF8784B152496318F87BCB674E344B97C0446B16D4DC2BDC381D88C048B9'。

所以,它确实指向了与SA_Locations有关的东西,正如我所说,SA_Location是实体框架数据库首先生成的一个类。。。。

越来越近,但不知道为什么该对象不会序列化?

ApiController返回504-服务器没有返回对此请求的完整响应.服务器返回了0个字节

这一切都归结为配置。ProxyCreateEnabled=false;

https://stackoverflow.com/a/12869077/1153795

HTH