First Last

First Last

  • NA
  • 648
  • 72.3k

Consume a web api in MVC - passing message but not receiving the same.

Feb 25 2022 1:28 AM

To consume a Web API in ASP.NET MVC server side I am using h t t p c l i e n t. It sends a request to the Web API and receives a response. Then I convert the response data that came from the Web API to a model and then render it into a view.

The problem is when there is an error in the web api, my client is not receiving the error that was sent. The web api sends a status code = 400 and a ReasonPhrase with my custom message, but when it gets to the client, it is a status code = 500 and the ReasonPhrase = 'Internal Server error'.

Why?

Note: this is rediculous! To inlcude the word h t t p c l i e n t in the above paragraph, I had to space it out otherewise I would get: the thread allready exists or there was some error!

Web api code:

  When I try to insert the code snippet so I can show it, it keeps telling me that the thread allready exists or there was some error!

  Makes no sense. I just use the code snippet icon, select C# and paste in my method code. SO WHAT IS WRONG!!

  I tried just to paste the code here. Still the same error.

So here is my debug screenshot.The error being sent (a status code = 400 ReasonPhrase with my custom message):

The client code:

  When I try to insert the code snippet so I can show it, it keeps telling me that the thread allready exists or there was some error!

  Makes no sense. I just use the code snippet icon, select C# and paste in my method code. SO WHAT IS WRONG!!

  I tried just to paste the code here. Still the same error.

So here is my debug screenshot.The error being received by the client (a status code = 400 and the ReasonPhrase = 'Internal Server error'.):

 

 


Answers (3)