c# - 在 IQueryable 上返回 204 状态代码Web API 操作
问题描述
web api
我正在使用toJSON
和custom CVS
解析器导出大量数据。一切正常,但我想204 status code
在查询返回 0 条记录时返回。我没有找到一种方法来设置状态码,因为我IQueryable<Log>
在行动中返回。有什么建议吗?
[HttpGet]
[Route("user/statistic")]
public IQueryable<Log> Statistic(int userId, DateTime startDate, DateTime endDate, CancellationToken cancellationToken)
{
var logs = _context.Find(userId, startDate, endDate);
return logs;
}
public IQueryable<Log> Find(int userId, DateTime startDate, DateTime endDate)
{
var startDateSql = startDate.AddDays(-1).Date;
var endDateTimeSql = endDate.AddDays(1).Date;
return Logs.Where(w => w.UserId == userId && w.DateStamp > startDateSql && w.DateStamp < endDateTimeSql).AsNoTracking();
}
解决方案
尝试使用IActionResult
而不是IQueryable
你的 API 方法,那么你可以有这样的东西:
public IActionResult MyApi()
{
// if the result has any item
return Ok(result);
// otherwise
return NoContent(); // which returns 204 status code
}
推荐阅读
- c# - 使用来自 MySql 的 2 个不同的表登录,它们有自己不同的窗口形式
- vba - 如果值等于 2,则复制整行并粘贴到工作表中
- flutter - Isolate 的入口函数不能标记为异步
- node.js - 如何获取节点 js 进程的百分比?
- powershell - 挂载 VHD 并获取驱动器号
- javascript - 从对象数组中返回匹配的对象
- java - 为 JUnit 测试触发“MouseEvent e”
- google-analytics - 谷歌分析跳出率太低
- javascript - 单击其他父菜单项时如何关闭所有其他打开的子菜单?
- html - 如何更改 Chrome OS 应用程序中的字体?