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 ???