ASP.NET Core Web API没有返回完整的对象。

huangapple go评论58阅读模式
英文:

ASP.NET Core Web API is not returning full object

问题

以下是翻译好的部分:

Model(模型):

public class ReconResults
{
    public IEnumerable<int> ReconIds { get; set; }
    public IEnumerable<ReconErrorResults> Messages { get; set; }
    public bool Success { get; set; }
}

public class ReconErrorResults
{
    int ErrNo { get; set; }
    string ErrorMessage { get; set; }
}

Repo(数据访问层):

using (var connection = sqlProvider.GetConnection())
{
    var aresults = connection.QueryMultipleAsync("recon.usp_pro",
        parameters,
        commandTimeout: 1500,
        commandType: CommandType.StoredProcedure).Result;

    results.Messages = await aresults.ReadAsync<ReconErrorResults>();
    results.ReconIds = await aresults.ReadAsync<int>();
    results.Success = (parameters.Get<int>("@Return") == 0 ? true : false);
}

Results(结果):

{
  "reconIds": [
    -1
  ],
  "messages": [
    {}
  ],
  "success": false
}
英文:

I have an ASP.NET Core Web API returning ReconResults. The API for some reason is not sending me the messages part of the object. I see the values of the messages prior to leaving the API. I am sure I am missing something pretty dumb.

Model:

public class ReconResults
{
    public IEnumerable&lt;int&gt; ReconIds { get; set; }
    public IEnumerable&lt;ReconErrorResults&gt; Messages { get; set; }
    public bool Success { get; set; }
}

public class ReconErrorResults
{
    int ErrNo { get; set; }
    string ErrorMessage { get; set; }
}

Repo:

using (var connection = sqlProvider.GetConnection())
{
    var aresults = connection.QueryMultipleAsync(&quot;recon.usp_pro&quot;,
        parameters,
        commandTimeout: 1500,
        commandType: CommandType.StoredProcedure).Result;

    results.Messages = await aresults.ReadAsync&lt;ReconErrorResults&gt;();
    results.ReconIds = await aresults.ReadAsync&lt;int&gt;();
    results.Success = (parameters.Get&lt;int&gt;(&quot;@Return&quot;) == 0 ? true : false);
}

Results

{
  &quot;reconIds&quot;: [
    -1
  ],
  &quot;messages&quot;: [
    {}
  ],
  &quot;success&quot;: false
}

答案1

得分: 5

Your ReconErrorResults properties should be marked as Public if you intend to use them outside the ReconErrorResults class...

public class ReconErrorResults 
{ 
    public int ErrNo { get; set; } 
    public string ErrorMessage { get; set; } 
} 
英文:

Your ReconErrorResults properties should be marked as Public if you intend to use them outside the ReconErrorResults class...

public class ReconErrorResults
{
    public int ErrNo { get; set; }
    public string ErrorMessage { get; set; }
}

huangapple
  • 本文由 发表于 2023年3月7日 03:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定