【问题标题】:ASP NET Core Minimal API always returns 200 instead of specified 204ASP NET Core Minimal API 总是返回 200 而不是指定的 204
【发布时间】:2021-09-17 07:31:55
【问题描述】:

即使我说返回 204 (NoContent),它也会返回 200。为什么?

app.MapGet("/api/alive", (context) => Task.FromResult(new StatusCodeResult(204)));

浏览器中的状态码是200

【问题讨论】:

  • 您能否显示更多您的代码,以便我们了解您是如何收到此回复的。通常我希望看到Return NoContent();
  • 使用 Results.NoContent()、new NoContentResult() 或 new StatusCodeResult(204) 没有区别
  • 你检查documentation了吗?你知道第二个参数的签名(RequestDelegate) 吗?你从哪里得知它有返回类型?
  • 将类型更改为 RequestDelegate 成功了
  • 不要将答案编辑到您的问题中。我已将您的问题回滚到原始版本。

标签: c# asp.net-core .net-6.0 asp.net-core-6.0


【解决方案1】:

解决方案是稍微更改签名:

app.MapGet("/api/alive", () => Results.NoContent());

(context) 单独不起作用, 编辑:以及(RequestDelegate 上下文)不起作用。

app.MapGet("/api/alive", (context) => Results.NoContent());
app.MapGet("/api/alive", (RequestDelegate context) => Results.NoContent());

【讨论】:

  • 有人知道为什么 RequestDelegate 不起作用吗?
猜你喜欢
  • 2012-03-09
  • 1970-01-01
  • 2022-10-20
  • 2017-08-09
  • 2020-02-16
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2021-06-18
相关资源
最近更新 更多