You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minimal API中实现带StatusCode和Problem的返回结果等价方案问询

Minimal API等价实现方案

当然有结构一致的等价实现方式,Minimal API里可以直接使用Results静态类提供的对应方法来实现相同逻辑:

return result.StatusCode == (int)HttpStatusCode.Created
    ? Results.StatusCode(result.StatusCode, result.MessageCode)
    : Results.Problem(detail: result.MessageCode, statusCode: result.StatusCode);

关键说明

  • Minimal API依赖Microsoft.AspNetCore.Http.Results静态类,它提供的StatusCode、Problem方法和ControllerBase中的对应方法行为完全一致。
  • 如果想更贴合Created状态码的语义,也可以用Results.Created方法,但要严格对齐原逻辑结构的话,用StatusCode方法更匹配。
  • Problem方法的参数和ControllerBase中的版本兼容,这里通过detail参数传入MessageCode,和原实现的参数传递逻辑对应。

内容的提问来源于stack exchange,提问作者Lorena Sfăt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:37:08