【问题标题】:Getting null values in Postman. How to fix it?在 Postman 中获取空值。如何解决?
【发布时间】:2019-11-06 19:34:35
【问题描述】:

我正在尝试测试这个方法:

public HttpResponseMessage Get()
{
    Orders orders = new Orders();
    var result = Request.CreateResponse(HttpStatusCode.OK, orders);

    if (orders.DateOrder > DateTime.Now || orders.DateOrder == null)
    {
        result.Content = new StringContent(JsonConvert.SerializeObject(db.Orders.ToList()));
       result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    }
    return result;
}

但我得到了奇怪的空值,而不是表中的空值。如何解决?也许是因为我在我的 MVC 项目中使用 Web API 而不是创建一个新的 Web API 项目?我希望你能帮助我。

image

【问题讨论】:

  • 您是否调试过订单是否填写了值
  • 我有一个包含我的值的 Orders 表,但 Postman 输出的是空值
  • 忘掉邮递员吧。调试你自己的端点,先看看发生了什么

标签: c# asp.net-web-api postman


【解决方案1】:

我认为问题在于你设置了var result = Request.CreateResponse(HttpStatusCode.OK, orders);

太早了。

orders 是一个空对象,因此它具有您从控制器获得的值。

您可以先检查if,并且只有在创建响应对象之后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2019-11-03
    • 2021-07-22
    相关资源
    最近更新 更多