为什么 asp.net core 发送空对象作为响应? [英] Why asp.net core sending empty object as response?

查看:65
本文介绍了为什么 asp.net core 发送空对象作为响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 VS 中调试代码时,我返回的城市列表中包含 3 个对象以及属性.当我调用此端点时,我收到了 3 个空对象列表项的响应.

如何解决这个问题?

模型类:

公共类城市{公共字符串城市名称;公共字符串 AssociatedCities;公串省;公共 int 状态;公共城市(字符串城市名称,字符串相关城市,字符串省,整数状态){this.CityName = cityName;this.AssociatedCities = associatedCities;this.Province = 省;this.Status = 状态;}}

端点:

[HttpGet][路线(城市")]公共 ActionResult>获取城市(){返回确定(城市);}

这就是我调用端点的方式

getCities() {this.http.get('/api/wizard/cities').订阅(结果 => {控制台日志(结果);this.cities = 结果;}, 错误 =>console.error('出了点问题:' + error));}

我得到的回应:

需要的响应:

<预><代码>[{"SearchCity": "多伦多",相关城市":阿贾克斯、惠特比、多伦多、密西沙加、布兰普顿","省": "开",状态":1},{"SearchCity": "温哥华","AssociatedCities": "温哥华,温哥华市","省": "BC",状态":1}]

我已经试过了:新的 ASP.NET CoreAPI 返回空的 JSON 对象

解决方案

基于这样一个事实,您的所有操作都会返回 Cities,这大概是您的控制器上定义的属性或字段,我我将在黑暗中试一试,并假设您在另一个请求中设置了它,并希望它仍然存在于这个请求中.这不是它的工作原理.控制器会随每个请求一起实例化和处理,因此在请求的生命周期内设置给它的任何内容都将无法生存.结果,Cities 在这个请求中没有任何内容,所以你得到一个空的响应.

如果您需要行动中的城市列表,那么您应该查询那些行动中的城市.此外,就其价值而言,System.Text.Json 目前不支持序列化字段,正如其他人在评论中提到的那样,但您仍然可以改用 JSON.NET,它确实支持.请参阅:https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#jsonnet-support

When I debug the code in VS, the cities list, which I am returning have 3 objects in it along with the properties. When I call this endpoint I am receiving a response of 3 list items of empty objects.

How to resolve this issue?

Model Class:

public class City
{
    public string CityName;
    public string AssociatedCities; 
    public string Province;
    public int Status;

    public City(string cityName, string associatedCities, string province, int status)
    {
        this.CityName = cityName;
        this.AssociatedCities = associatedCities;
        this.Province = province;
        this.Status = status;
    }
}

Endpoint:

[HttpGet]
[Route("cities")]
public ActionResult<IEnumerable<City>> GetCities()
{
    return Ok(Cities);
}

This is how I am calling the endpoint

getCities() {
  this.http.get<City[]>('/api/wizard/cities')
  .subscribe(result => {
    console.log(result);
    this.cities = result;
  }, error => console.error('Something went wrong : ' + error));
}

The response I get:

The response that is needed:

[
  {
    "SearchCity": "Toronto",
    "AssociatedCities": "Ajax, Whitby, Toronto, Mississauga, Brampton",
    "Province": "ON",
    "Status": 1
  },
  {
    "SearchCity": "Vancouver",
    "AssociatedCities": "Vancouver, Vancouver City",
    "Province": "BC",
    "Status": 1
  }
]

I have tried this already: Fresh ASP.NET Core API returns empty JSON objects

解决方案

Based on the fact that all your action does is return Cities, which presumably is a property or field defined on your controller, I'm going to take a shot in the dark and assume that you're setting that in another request and expecting it to still be there in this request. That's not how it works. The controller is instantiated and disposed with each request, so anything set to it during the lifetime of a request will not survive. As a result, Cities has nothing in this request, so you get an empty response.

If you need a list of cities in the action, then you should query those in that action. Also, for what it's worth, System.Text.Json does not currently support serializing fields, as others have mentioned in the comments, but you may still use JSON.NET instead, which does. See: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#jsonnet-support

这篇关于为什么 asp.net core 发送空对象作为响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆