空响应 returns a 204
Null response returns a 204
我的控制器 return 在我发出 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 return 200 空响应。这与我的 swagger 定义混淆了,它有一个为 200 响应定义的响应,我宁愿保持一致。
如果我在这个控制器之外 HTML 服务,204 会很好,但它是用于 REST API。
如何才能达到 return 200?
使用 v2.1+ 中的新 ActionResult<T>
,您还可以重构以使用 Ok()
辅助方法
专门告诉控制器 return Ok 200
[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);
}
}
然而,如果 return 实际上没有内容,这可能会产生误导。考虑使用适当的响应状态
[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
}
}
如果打算 returning 200 Ok 没有内容使用 ControllerBase.Ok()
方法
Creates a OkResult object that produces an empty Status200OK response.
[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
}
}
参见:
- https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.1#special-case-formatters
- https://www.colabug.com/2020/0224/7036191/
- https://weblog.west-wind.com/posts/2020/Feb/24/Null-API-Responses-and-HTTP-204-Results-in-ASPNET-Core
services.AddControllers(opt => // or AddMvc()
{
// remove formatter that turns nulls into 204 - No Content responses
// this formatter breaks Angular's Http response JSON parsing
opt.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
})
我的控制器 return 在我发出 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 return 200 空响应。这与我的 swagger 定义混淆了,它有一个为 200 响应定义的响应,我宁愿保持一致。
如果我在这个控制器之外 HTML 服务,204 会很好,但它是用于 REST API。
如何才能达到 return 200?
使用 v2.1+ 中的新 ActionResult<T>
,您还可以重构以使用 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);
}
}
然而,如果 return 实际上没有内容,这可能会产生误导。考虑使用适当的响应状态
[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
}
}
如果打算 returning 200 Ok 没有内容使用 ControllerBase.Ok()
方法
Creates a OkResult object that produces an empty Status200OK response.
[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
}
}
参见:
- https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.1#special-case-formatters
- https://www.colabug.com/2020/0224/7036191/
- https://weblog.west-wind.com/posts/2020/Feb/24/Null-API-Responses-and-HTTP-204-Results-in-ASPNET-Core
services.AddControllers(opt => // or AddMvc()
{
// remove formatter that turns nulls into 204 - No Content responses
// this formatter breaks Angular's Http response JSON parsing
opt.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
})