【发布时间】:2019-07-29 23:01:06
【问题描述】:
我如何始终返回 Json 响应,HTTP 代码是否需要为 200、401 或其他都无关紧要。
对于 HTTP 200,Json 使用 return Ok(response); //where response is a model. 发送
对于 HTTP 401(未经授权),我有以下内容:
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent(msg),
ReasonPhrase = msg
});
但是 msg 需要是一个字符串...我无法让它与模型一起返回 JSON。
BadRequest 或其他 HTTP 状态也会发生同样的情况。
【问题讨论】: