In one of my method I have to return IHttpActionResult
from there in Web API 2. When succeeded it returns Ok()
either it returns BadRequest()
with some message.
I am setting the message of BadRequest
in the following way. But the problem is that I am unable to retrieve the error message from the called method. I am using Visual Studio 2013 Update 4 and the version is MVC 5.
return BadRequest("Error! Error adding the device");
Please help. Thanks in advance.
After through research and study I found the solution. As this is a method of Web API, I have called it through my website. I have called it like the following way where RequestUrl
is the url of this Web API method. If there are parameter(s) in the method, we have to write it in querystring.
HttpResponseMessage Response = null;
Response = client.GetAsync(RequestUrl).Result;
var stream = Response.Content.ReadAsStreamAsync().Result;
// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
We also can have the response type using the following code.
Response.ReasonPhrase
And in this way I can have the message of BadRequest
as well Ok
. BINGO !!