Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to send web api response

$
0
0

Hello All,

We are implementing web api in which we are validating the client data like 'Incorrect name', 'Address not found'. So, I want to send these custom messages to the client and not the exceptions.

 if (dsNew.Tables[0].Columns.Contains("ErrMsgDesc") && dsNew.Tables[0].Rows[0]["ErrMsgDesc"] != null)
{
var msg = dsNew.Tables[0].Rows[0]["ErrMsgDesc"].ToString();
var result = new HttpResponseMessage(HttpStatusCode.NotFound);
result.ReasonPhrase = msg;
return result;
}

But above response is getting catch in Try.. Catch block Instead I want to get this response in some variable

WebAPi Calling Code

try
                {
                    Stream requestStream = request.GetRequestStream();

                    requestStream.Write(postBytes, 0, postBytes.Length);
                    requestStream.Close();

                    var response = (HttpWebResponse)request.GetResponse(); // Here I want to get the response in varible.

                    return response.ToString();

                }
                catch (Exception ex)
                {
                   
                    request.Abort();
                    return string.Empty;
                }

Instead of getting in variable it is going to Catch block. If we do not add the HttpStatusCode; then it is working fine.

So, How can I solve this problem ???


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>