Web API 给出空响应
Web API giving null in response
我开发了一个web api
,它接受两个日期时间参数(开始和结束),然后应该给出不同的记录。
public HttpResponseMessage GetMeterPing(DateTime start, DateTime end)
{
try
{
var startDateTime = start;
var endDateTime = end;
var result = medEntitites.tj_xhqd.Where(m => m.sjsj >= startDateTime && m.sjsj <= endDateTime)
.OrderByDescending(o => o.sjsj)
.Select(s => new { s.zdjh, s.sjsj, s.xhqd })
.Distinct()
.FirstOrDefault();
return Request.CreateResponse(HttpStatusCode.OK, new { data = result });
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
API URL: http://localhost:14909/api/meters/GetMeterPing/2018-04-28T00:00:00/2018-04-27T23:59:59
当我 运行 这个 web-api
它给了我
{"data":null}
同样在调试时 result
也是 null
任何帮助将不胜感激
你对问题的描述听起来像是你正在执行的 Linq 查询没有返回任何结果,所以你对 FirstOrDefault
的调用是默认的,也就是说它返回 null。然后,您不执行任何其他验证,并使用此行的结果作为具有 data
属性.
的匿名投影的值进行响应
我开发了一个web api
,它接受两个日期时间参数(开始和结束),然后应该给出不同的记录。
public HttpResponseMessage GetMeterPing(DateTime start, DateTime end)
{
try
{
var startDateTime = start;
var endDateTime = end;
var result = medEntitites.tj_xhqd.Where(m => m.sjsj >= startDateTime && m.sjsj <= endDateTime)
.OrderByDescending(o => o.sjsj)
.Select(s => new { s.zdjh, s.sjsj, s.xhqd })
.Distinct()
.FirstOrDefault();
return Request.CreateResponse(HttpStatusCode.OK, new { data = result });
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
API URL: http://localhost:14909/api/meters/GetMeterPing/2018-04-28T00:00:00/2018-04-27T23:59:59
当我 运行 这个 web-api
它给了我
{"data":null}
同样在调试时 result
也是 null
任何帮助将不胜感激
你对问题的描述听起来像是你正在执行的 Linq 查询没有返回任何结果,所以你对 FirstOrDefault
的调用是默认的,也就是说它返回 null。然后,您不执行任何其他验证,并使用此行的结果作为具有 data
属性.