7

I'm creating a DataAnnotation that if not successful will return a BadRequest.

context.Result = new BadRequestResult();

That works fine.

I want to include a message along with this, but BadRequestResult doesn't appear to take any message params?

Is there a way to do this...include a message to say e.g. "reference number not supplied"

Thanks

thegunner
  • 6,883
  • 30
  • 94
  • 143
  • There's an overloaded constructor that takes a message, like [`BadRequestResult(HttpRequestMessage)`](https://msdn.microsoft.com/en-us/library/system.web.http.results.badrequestresult(v=vs.118).aspx#M:System.Web.Http.Results.BadRequestResult.#ctor(System.Net.Http.HttpRequestMessage)) – juanferrer Jul 03 '17 at 14:03

2 Answers2

12

This appears to work:

context.Result = new BadRequestObjectResult("Reference number not supplied.");
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
thegunner
  • 6,883
  • 30
  • 94
  • 143
1

You can use ApiController.BadRequest("Reference number not supplied") to pass a message along with the result. Here's the documentation in MSDN.

juanferrer
  • 1,192
  • 1
  • 16
  • 29