返回GET请求异常过滤器上的对象

时间:2016-12-26 11:13:25

标签: asp.net

我试图通过ASP.NET WEb API调用返回带有json数据的对象 我想以IHttpActionResult

的形式返回数据
{
  "message": "success",
  "statuscode": "SuccessCode",
  "statusmessage": "SuccessMsg",
  "data": [
    {
      "corrBD1": null,
      "flagType1": null,
      "status1": null,
      "mstrRep1": null,
      "repname1": null,
      "nameAccount1": null,
      "acDate1": "0001-01-01T00:00:00",
      "preAddress1": null,
      "currAddress1": null
    }]
}

我可以在有效的GET请求中返回此数据:

return OK(employee);

但是当发生异常时,我需要以相同的形式返回数据

    {
  "message": "fail",
  "statuscode": "400",
  "statusmessage": "failmsg",
  "data": null
}

但异常过滤器强制我以HttpResponseMessage的形式发送回复(遗憾的是无法以所需形式返回) 怎么做到这一点?

2 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,那么无论您是在处理控制器还是异常过滤器中的响应,您只是尝试在结果中使用一致的消息语法。你应该能够通过创建自己的IHttpActionResult来做你想做的事情:

private class MyResult : IHttpActionResult
    {
        public HttpRequestMessage Request { get; set; }

        public string Content { get; set; }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response = 
               new HttpResponseMessage(HttpStatusCode.InternalServerError); // or whatever
            response.Content = new StringContent(Content); // your content here
            response.RequestMessage = Request;
            return Task.FromResult(response);
        }
    }

在您的处理程序中,您只需将ExceptionHandlerContext.Request设置为您在上面创建的实例。这对你的情况有用吗?

答案 1 :(得分:0)

谢谢杰里米!我已经实施了您的解决方案我已对其进行了一些更改,并能够实现所需的结果。
请检查代码并提出建议。

   public class CustomExceptionResult : IHttpActionResult
        {
            private HttpRequestMessage _request;
            private HttpResponseMessage _httpResponseMessage;


            public CustomExceptionResult(HttpRequestMessage request, HttpResponseMessage httpResponseMessage, Employee data)
            {
                _request = request;                

                _httpResponseMessage = request.CreateResponse(HttpStatusCode.InternalServerError, data);

            }

            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                return Task.FromResult(_httpResponseMessage);
            }
        }


这是我的异常处理程序代码。

    public class NotFoundExceptionFilter:ExceptionFilterAttribute
    {

        public override void OnException(HttpActionExecutedContext context)
        {
            if (context.Exception is KeyNotFoundException)
            {
                var msg = context.Exception.Message;
//class for creating standard Json object
                JsonWrapper data= new JsonWrapper("There is some problem with the key you are looking for","400","Bad Request",null);
                Controllers.NotFoundExceptionResult result = new Controllers.NotFoundExceptionResult(context.Request, data);
                context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, result);                
            }
            else
            {
                base.OnException(context);
            }
        }
    }


我在结果中获得了以下Json:

{
  "message": "There is some problem with the key you are looking for",
  "statuscode": "400",
  "statusmessage": "Bad request",
  "data": null
}