paschka76
paschka76

Reputation: 95

ASP.NET Web API 2 BadRequest content

I'm not receiving the expected Response Content on the client when the resource returns BadRequest.

[HttpGet]
[Route("Test", Name = "Test")]
public async Task<IHttpActionResult> Test()
{
    var result = BadRequest("test");
    return result;
}

On client (see hurl.it example below) I simply receive the string Bad Request in the body: Bad Request string in content

The response on the server seems to be fine: Response server side

It was working fine at some point (returning strings or ModelState in content) and recently we noticed this problem. I can't think of any recent change on server that could cause it.

It works neither locally nor when deployed on server.

It can be reproduced in any ApiController in the project.

return Ok("test"); works as expected.

Does anyone know what can cause this behavior?

Thank you!

Upvotes: 2

Views: 3243

Answers (1)

CularBytes
CularBytes

Reputation: 10321

It is hard to tell what goes wrong.

Things you could check:

  • Perhaps is your error caused by invalid authentication request
  • Try with a new project, if that makes difference then you know it's your project and there are no errors caused by your local IIS and server settings (highly unlikely but you never know.
  • Check your App_Start folder, containing the BundleConfig, RouteConfig, FilterConfig,WebApiConfig`. Perhaps some custom settings did cause to give you bad request error while it might be a not found error.
  • Check if it's only on Get request or also on others, could be caused by different versions of assemblies.
  • Check if you only have the problem with 400, or does 401, 500 gives the same problem?
  • Check your Web.Config file, these might contain <CustomErrors> that might redirect, or throw there own errors.

After some comments, custom erros seemed to be the problem.

Upvotes: 1

Related Questions