我有以下行動: [Route("GetNull")] [HttpGet] public IActionResult GetNull() { object value = new { Name = "John" }; return Ok(value); }它返回序列化的對象:HTTP/1.1 200 OKDate: Tue, 12 Jun 2018 06:13:05 GMTContent-Type: application/json; charset=utf-8Server: KestrelContent-Length: 22{ "name": "John"}如果對象像這樣設(shè)置為 null: [Route("GetNull")] [HttpGet] public IActionResult GetNull() { object value = null; return Ok(value); }我希望返回 JSON 中的 null。但是,我得到這樣的空內(nèi)容:HTTP/1.1 204 No ContentDate: Tue, 12 Jun 2018 06:17:37 GMTServer: KestrelContent-Length: 0我確實(shí)讓控制器將 null 序列化為 json 并返回:HTTP/1.1 200 OKDate: Tue, 12 Jun 2018 06:13:05 GMTContent-Type: application/json; charset=utf-8Server: KestrelContent-Length: 4null
2 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個贊
如Microsoft 所述,您可以從控制器返回空 JsonResult。
例如,
return Json(null);
但是,更好的方法可能是返回 OK 狀態(tài),如果這就是您所需要的。
return Ok();
- 2 回答
- 0 關(guān)注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消