Howiecamp
Howiecamp

Reputation: 3111

ASP.NET Web API not returning custom error information

I have an ASP.NET Web API 2 action method:

[System.Web.Http.HttpPost]
public HttpResponseMessage Create(HttpRequestMessage req)
{
   //...

   if (success)
      return Request.CreateResponse(HttpStatusCode.Created);

   return CreateErrorResponse(HttpStatusCode.BadRequest, "you done bad");
}

Until I did "something", upon error it would return http 400 with the custom error text "you done bad". That's the expected result.

It no longer returns the custom text; it just returns the standard "Bad request". Have been trying to understand what changed to make this happen.

So I tried:

var response = new { message = "you done bad" };
return Request.CreateResponse(HttpStatusCode.BadRequest, response);

Same result.

I then created a new, clean Web API project and I get the result I expect.

How did I break my project?

Upvotes: 1

Views: 3210

Answers (3)

Howiecamp
Howiecamp

Reputation: 3111

The problem is related to the CustomErrors configuration in web.config. From @HaukurHaf's answer at Error messages returned from Web API method are omitted in non-dev environment:

Had the same issue. It's indeed because of the custom errors setting.

In a real world scenario, you would definitely want to use a custom error page in your application, but in order for custom exception messages to work in the WebAPI you need to disable the custom errors page.

How to fix this? Luckily, you can use the <location> element in your web.config to solve this.

Solution:

 <!-- General for the application -->
  <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="YourCustomErrorPage.aspx"/>
  </system.web>

  <!-- Override it for paths starting with api (your WebAPI) -->
  <location path="api">
     <system.web>
        <customErrors mode="Off" />
     </system.web>
  </location>

I use this method in my own app, works well.

While I do see that this works, I don't understand why the CustomErrors section breaks it.

Upvotes: 5

woodykiddy
woodykiddy

Reputation: 6455

I think you can probably consider creating a instance of HttpResponseMessage and add your custom error message to its Content property.

HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.BadRequest);
msg.Content = new StringContent("you done bad");
return msg;
// or can re-throw it
//throw new HttpResponseException(msg);

Please check the following link for details

https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx

Upvotes: 0

Asif Raza
Asif Raza

Reputation: 1021

You need to change your method as below

    [Route("User/GetUserIDCustomMessage")]
    [HttpPost]
    public HttpResponseMessage GetUserIDCustomMessage(HttpRequestMessage request, int Id)
    {
        HttpResponseMessage response = null;
        if (Id == 1)
        {
            response = request.CreateResponse(HttpStatusCode.OK);
        }
        else {

            response = request.CreateResponse(HttpStatusCode.BadRequest , "YOu type bad");

        }
        return response;
    }

When I give 1 , it will give me OK message with 200 status code , otherwise it will give 400/Bad Request , with body message 'YOu type bad'

Upvotes: 0

Related Questions