6

I'd like to return a data object that contains the details of the error with a BadRequestErrorMessageResult or BadRequestErrorMessageResult object like so:

public IHttpActionResult Action(Model model)
{
  var validationResult = model.Validate();
  if (validationResult.Successful)
  {
    // this one's okay; it supports sending data with a 200
    return Ok(validationResult);
  }
  else
  {
    // However, how do I return a custom data object here
    // like so?
    // No such overload, I wish there was

    // return BadRequest(validationResult);
  }
}

The only three overloads of the ApiController.BadRequest() method are:

1. BadRequest();
2. BadRequest(string message);
3. BadRequest(ModelStateDictionary modelState);

Even with #3, a model state dictionary is ultimate a deep collection with one layer upon another, at the bottom of which, though, is a bunch of KeyValuePair<string, ModelError> where each ModelError also only has either a string or an Exception object.

Therefore, even with #3, we are only able to pack a string to send and not a custom object like I want to.

I am really not asking how I may go about working a hack or a kludge around the situation. My question is: is there an overload or another way baked into the .NET API to send an object to the client with a Bad Request HTTP status code?

I am using ASP.NET Web API version 5.2.4 targeting .NET Framework version 4.6.1.

2
  • take your custom object, serialise it and use the second overload to return it. Feb 19, 2019 at 8:10
  • @AndreiDragotoniu Thank you. That's one of the things I am already considering if there isn't a way baked in into the .NET framework. Feb 19, 2019 at 8:16

2 Answers 2

9

You can use the Content<T>(...) method to do this. It returns a NegotiatedContentResult, which is serialized depending on the request headers (e.g. json, xml), and allows you to specify a HttpStatusCode.

You can use it like this:

return Content(HttpStatusCode.BadRequest, myObject);

If you wanted to, you could create your own BadRequest<T>(T obj) method in the controller as a wrapper, so then you could call it as you wanted:

public IHttpActionResult BadRequest<T>(T obj)
{
    return Content(HttpStatusCode.BadRequest, obj);
}

public IHttpActionResult Action()
{
    // do whatever validation here.
    var validationResult = Validate();

    // then return a bad request
    return BadRequest(validationResult);
}
2
  • Wow! just tried it in an example project and it works. Why didn't I look it up? Many thanks. Jul 1, 2019 at 6:57
  • 1
    No worries, I only just found it myself because I had a similar problem! I was looking at doing all kinds of convoluted things before I stumbled across this method. Jul 1, 2019 at 7:03
0

You can build/format the string in JSON format, pass it as string in the BadRequest() parameter and convert it to JSON again or any object on the caller's backend.

Haven't tried that but that should work.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.