7

The controller now has function CreatedAtRoute() for 201, HttpBadRequest() for 400, etc. I don't see one for 500 which I figure would be HttpInternalServerError().

There is, however the HttpStatusCodeResult class which I can create and return:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
   ...
    try{
    }
    catch(Exception e)
    {
         return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
    }
}

But I want to return some info from e. This might be bad practice for live, but when we're testing, we'd like to see the errors from the returned body of the API call.

HttpStatusCodeResult does not have an property of type object or anything for the purpose of providing metadata.

What to do? I don't want to return a 400 because that's not what an Internal Server Error means.

3
  • I can't say for asp.net, but in many frameworks for Java there is often some type of HTTPResponse object that encapsulates both a response code and a response body. Might be something worth looking into. Commented Dec 10, 2015 at 16:04
  • Use the HttpStatusCodeResult(HttpStatusCode, String) overload to provide a description. As far as passing an object, you're right--you can't. But that's per the protocol. Commented Dec 10, 2015 at 16:05
  • 1
    @CollinD - there doesn't seem to be a constructor with that second string parameter. I might be missing a reference to an extension. Commented Dec 10, 2015 at 16:12

1 Answer 1

5
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Message describing the error here"));

or

return InternalServerError(new Exception("SOME CUSTOM MESSAGE"));
Sign up to request clarification or add additional context in comments.

1 Comment

I would to the later, since it's a Web Api 2.x approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.