【发布时间】:2017-02-27 17:22:10
【问题描述】:
我想从 ApiController 的 PUT 方法返回 HTTP 状态代码和错误消息。
我创建了一个错误类:
public class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
在 Put 方法中我返回:
return new ResponseMessageResult(Request.CreateResponse(HttpStatusCode.NotModified,
new ErrorInformation { Message = "We apologize but an unexpected error occured. Please try again later.", ErrorDate = DateTime.UtcNow }));
当状态码是,说:
HttpStatusCode.OK 或 HttpStatusCode.InternalServerError - 调用者在响应中获取对象。但是当它是HttpStatusCode.NotModified 时 - 只返回状态码。
为什么会这样 - 我如何确保始终返回我的对象和状态代码?
谢谢。
【问题讨论】:
-
无论如何,这对于“未修改”状态代码的使用都是无效的,尤其是对于类似“我们道歉但发生意外错误”这样的消息。请稍后再试。”
标签: c# asp.net-web-api asp.net-web-api2