Web API 2 - Use of Reasonphrase in exception handling

Asked

Viewed 398 times

1

var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
    ReasonPhrase = "Nenhum produto encontrado"
};

throw new HttpResponseException(resp);

That code should return

404 No products found

But for some reason it’s not working. Any ideas? I’m using Visual Studio 2013 and IIS Express.

Example print (ignore the URL and the use of HEAD, was just to illustrate)

inserir a descrição da imagem aqui

  • 1

    I think it’s your test interface that doesn’t read the ReasonPhrase. Have you tried testing using an Ajax request like this? http://www.codeguru.com/csharp/.net/asp/handling-exceptions-in-asp.net-web-api.htm

  • That’s right @Ciganomorrisonmendez...

  • In these situations the Fiddler is your best friend.

  • @Paulomorgado I suspected that it was something of the type, and now that confirmed I started to use it...

1 answer

1


Well, apparently the tip from Gypsy Morrison Mendez is correct, I did a small test using the JS below:

$.ajax({
    type: "GET",
    url: 'api/teste/?id=1',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        alert(result.CustomerID + " - " + result.CompanyName);
    },
    error: function (err, type, httpStatus) {
        alert(err.status + " - " + err.statusText + " - " + httpStatus);
    }
});

And then it worked, exhibited the Reasonphrase and the content of the answer. I will now use the Fiddler to avoid this kind of thing!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.