Return content with IHttpActionResult for non-OK response

前端 未结 15 2104
北恋
北恋 2020-11-28 17:55

For returning from a Web API 2 controller, I can return content with the response if the response is OK (status 200) like this:

    public IHttpActionResult          


        
相关标签:
15条回答
  • 2020-11-28 18:51

    For exceptions, I usually do

     catch (Exception ex)
            {
                return InternalServerError(new ApplicationException("Something went wrong in this request. internal exception: " + ex.Message));
            }
    
    0 讨论(0)
  • 2020-11-28 18:52

    Anyone who is interested in returning anything with any statuscode with returning ResponseMessage:

    //CreateResponse(HttpStatusCode, T value)
    return ResponseMessage(Request.CreateResponse(HttpStatusCode.XX, object));
    
    0 讨论(0)
  • 2020-11-28 18:55

    Simple:

    return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Your message"));

    Remember to reference System.Net.Http and System.Net.

    0 讨论(0)
提交回复
热议问题