HttpError和IHttpActionResult

时间:2014-05-23 03:02:37

标签: c# asp.net .net asp.net-mvc asp.net-web-api

如何返回HttpError(自己的,相当格式化的前端错误消息)然后我使用方便的IHttpActionResult

public IHttpActionResult Delete(Guid id)
{
    HttpError error = null;

    // code omitted

    if (error != null) {
        // How can I achieve that?
        return BadRequest(error);
    }

    return Ok();
}

1 个答案:

答案 0 :(得分:9)

您可以使用ResponseMessageResult

public IHttpActionResult Error()
{
    var error = new HttpError();
    return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, error));
}