空响应返回 204

2024-04-04

当我执行 GET 请求但没有找到任何数据时,我的控制器返回 204。

[Route("user/v1/[controller]")]
public class UserLoginController : Controller
{
    [HttpGet]
    public async Task<UserLogin> Get(int userId)
    {
        var userLoginLogic = new UserLoginLogic();

        return await userLoginLogic.GetUserLogin(userId);
    }
}

这仅适用于 GET 请求,POST、PUT、DELETE 返回 200 空响应。这与我的 swagger 定义相混淆,该定义为 200 响应定义了响应,我宁愿保持一致。

如果我从此控制器中提供 HTML,那么 204 就可以了,但它是用于 REST API 的。

我怎样才能让它返回200?


随着新ActionResult<T>在 v2.1+ 中,您还可以重构以明确告诉控制器返回 Ok 200 使用Ok()辅助方法

[Route("user/v1/[controller]")]
public class UserLoginController : Controller {
    [HttpGet]
    public async Task<ActionResult<UserLogin>> Get(int userId) {
        var userLoginLogic = new UserLoginLogic();
        var model = await userLoginLogic.GetUserLogin(userId);
        return Ok(model);
    }
}

然而,如果实际上没有内容可返回,这可能会产生误导。考虑使用适当的响应状态

[Route("user/v1/[controller]")]
public class UserLoginController : Controller {
    [HttpGet]
    public async Task<ActionResult<UserLogin>> Get(int userId) {
        var userLoginLogic = new UserLoginLogic();
        var model = await userLoginLogic.GetUserLogin(userId);
        if(model == null) return NotFound(); //404
        return Ok(model); //200
    }
}

如果打算返回 200 Ok 且不使用任何内容ControllerBase.Ok() https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.ok?view=aspnetcore-2.2#Microsoft_AspNetCore_Mvc_ControllerBase_Ok method

创建一个 OkResult 对象,该对象生成空的 Status200OK 响应。

[Route("user/v1/[controller]")]
public class UserLoginController : Controller {
    [HttpGet]
    public async Task<ActionResult<UserLogin>> Get(int userId) {
        var userLoginLogic = new UserLoginLogic();
        var model = await userLoginLogic.GetUserLogin(userId);
        if(model == null) return Ok(); //200 with no content
        return Ok(model); //200
    }
}

参考ASP.NET Core Web API 中的控制器操作返回类型: https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1#actionresultt-type

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

空响应返回 204 的相关文章

  • 使用 Html Agility Pack 获取 html 页面上的所有 div id

    如何使用 Html Agility Pack 获取 html 页面上的所有 div id 我正在尝试获取所有 id 并将它们放入一个集合中 p p div class myclass1 div div div div div div div
  • Automapper、Mapper 未初始化。使用正确的配置调用初始化

    当我尝试将数据提交到数据库时 出现以下错误 Success false Error true ErrorType 2 Message System InvalidOperationException Mapper 未初始化 使用适当的配置调
  • 在 XML 中,带问号的节点叫什么?在 C# 中如何添加它们?

    以下是在 InfoPath 中创建的 XML 文件的示例
  • 处理器关联组 C#

    我使用的是 72 核的 Windows Server 2016 我看到有两组处理器 我的 net 应用程序将使用一个或其他组 我需要能够强制我的应用程序使用我选择的组 我看到下面的代码示例 但我无法使其工作 我可能传递了错误的变量 我希望应
  • 如何在 ASP.NET MVC 中处理会话数据

    假设我想存储一个名为language id在会议中 我想我也许可以做如下的事情 public class CountryController Controller WebMethod EnableSession true AcceptVer
  • 图片框、双击和单击事件

    我有一个奇怪的问题 我有一个图片框双击事件以及单击事件 问题是即使我双击该控件 也会引发单击事件 如果我禁用单击事件 则双击事件正在工作 这个问题已经在这里讨论过 https stackoverflow com questions 1830
  • 如何将 dll 中包含的组件嵌入到 exe 中,以便它可以从内存运行?

    我正在尝试制作一个必须从内存运行的程序 通过Assembly Load bin 如上所述here http www codeproject com Articles 13897 Load an EXE File and Run It fro
  • MVC BaseController 处理 CRUD 操作

    我想重构我的基本 CRUD 操作 因为它们非常重复 但我不确定最好的方法 我的所有控制器都继承 BaseController 如下所示 public class BaseController
  • 使用c#在mac上启动外部进程

    我成功地使用 System Diagnostics Process Start 在 Windows 上启动我的外部单声道可执行文件 然而在mac上却失败了 我没有收到任何错误 只是什么也没发生 我尝试按以下方式进行操作 System Dia
  • 在编译输出中添加程序集绑定 (app.config)

    如果我编译应用程序 则会在输出中自动添加程序集绑定 具体的程序集绑定不在app config在 Visual Studio 中但在创建的应用程序配置中 有什么办法可以检查为什么会自动添加程序集绑定吗 选项AutoGenerateBindin
  • 为什么 xcode IDE 认为 `friend` 是保留字

    我一直在开发一个个人项目 并在我创建的新类中包含以下代码 property readonly getter isFriend BOOL friend 它似乎没有任何问题 当我构建它时 它可以编译得很好 但是当我们在xcode IDE看起来像
  • 将两个垂直滚动条相互绑定

    我在控件中有两个 TextBox 并且它们都有两个 VerticalScrollBar 我想在它们之间绑定 VerticalScrollBars 如果一个向上 第二个也会向上等等 如果可以的话我该怎么做 Thanks 不是真正的绑定 但它有
  • C 编程中的 rand() 问题? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么我总是用 rand 得到相同的随机数序列 https stackoverflow com questions 1108780 why do i always get the same seque
  • 对列表中的一系列整数求和

    假设我有一个这样的列表 List
  • 批量插入,asp.net

    我需要获取与会员相对应的 ID 号列表 在任何给定时间处理的数量可能在 10 到 10 000 之间 我可以毫无问题地收集数据 解析数据并将其加载到 DataTable 或任何内容 C 中 但我想在数据库中执行一些操作 将所有这些数据插入表
  • 如何通过分解 y 轴来减小 mschart 的高度

    如何降低 mschart 的高度 如下所示 编辑 就我而言 我不想查看中断图表 this chart1 ChartAreas 0 AxisY ScaleBreakStyle Enabled false 您似乎正在寻找AxisY ScaleB
  • ArrayList 有什么问题?

    最近我问了一个关于 SO 的问题 其中提到了可能使用 c ArrayList 来解决问题 有人评论说使用数组列表不好 我想了解更多有关此的信息 我以前从未听说过关于数组列表的这种说法 有人可以带我了解使用数组列表可能出现的性能问题吗 C n
  • 为什么使用 .AsEnumerable() 而不是转换为 IEnumerable

    扩展方法之一IEnumerable
  • 结构大小与 typedef 版本不同?

    我的代码中有以下结构声明和 typedef struct blockHeaderStruct bool allocated unsigned int length typedef struct blockHeaderStruct block
  • C# 中的 mshtml.HTMLDocumentClass

    在 C 中 我设法从 InternetExplorer 对象获取整个 HTMLDocumentClass 导航到某个 URL 然而 在 Visual Studio 2008 的调试模式下 该特定 URL 的 HTMLDocumentClas

随机推荐