Include your method as follows:
public HttpResponseMessage CheckDevice(string device) { try { devices = new List<DeviceClass>(); using (connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("uspCheckDeviceID", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@sp", SqlDbType.VarChar).Value = device; connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { if (reader.Read()) { DeviceClass model = new DeviceClass(); model.DeviceId = reader.GetValue(0).ToString(); model.Name = reader.GetValue(1).ToString(); model.Owner = reader.GetValue(2).ToString(); devices.Add(model); } } connection.Close(); } } } return Request.CreateResponse(HttpStatusCode.OK, devices.ToList()); } catch (Exception e) {
You can then get your Exception ToString when an error occurs.
To make sure you see the exception message, you can use Fiddler or PostMan . To test your API.
.
Also, I just try the way in the commentary on the OP question,
In my case, I am testing the return of some exception object defined by me,
initially, when I call this GET method by entering the URL in Google Chrome,
Chrome just gives me a meaningless message An error has occured ,
and after adding
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
with Global.asax.cs in the protected void Application_Start() method,
My Exceptional Object is displayed correctly.
.
In an hour, I will find out that this could also be done in WebApiConfig.cs in the public static void Register(HttpConfiguration config) method on
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;