简体   繁体   中英

Web API Should be returning custom JSON but returns back HTTP Status Code instead

I've discovered something peculiar that's been going on for the past 2 days.

I'm returning back custom JSON message in my Web API 2.0. Eg when there's been an Unauthorized Response (HttpStatusCode 401)..it's only returning back the HttpStatusCode and NOT the JSON.

Simply put, anything apart from a HTTP Status Code 200 is NOT returning back the custom JSON that I've explicitly put in my ActionHandlers to return back.

This has happened recently, in the past 2 days. I've tested my code locally and I'm getting the expected output I need..the custom JSON error messages but as soon as I publish to one of my slots whether it be production or dev..only HttpStatus Codes are being returned.

Can anyone verify this for me? I need a way to sort this issue out or atleast have Azure take a look at this and tell me what to do. I suspect it's been an Azure update which I'm unaware of.

Probably some kind of customErrors logic kicks in when you are remotely testing your site. Those logics are usually disabled when browsing locally. Check your configuration. Under IIS, there is also the httpErrors configuration which may interfere.

And have you set TrySkipIisCustomErrors to true when your code yields an error responses?

Ok, so I've figured out the issue.

Upgraded Visual Studio to 2015 to get the .NET Framework to use 4.6.

Changed all of my projects in the solution to point to 4.6, re-compiled, published and finally tested.

Errors are returning back JSON now too.

IMO, Azure could've atleast sent us an update. Anyways, posting this answer so hopefully anyone else using Web Api can easily forgo this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM