ASP.NET Web API An error has occurred

I am working on an ASP.NET Web API, and when I run this method, I get this error An error has occurred.

public List<DeviceClass> CheckDevice(string device) { 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 devices; } 

What I'm trying to determine if its code or its connection to the server.

This is a stored procedure in which I gave an example:

 declare @sp varchar(30) set @sp ='marksiphoneid' exec uspCheckDeviceID @sp 

Is there something wrong with the way I am trying to get the results from my stored procedure?

Error

Failed to load the resource: the server responded with a status of 500 (Internal server error)

+5
source share
2 answers

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) { //Request.CreateErrorResponse can do the same thing var err = new HttpRequestMessage(). CreateErrorResponse(HttpStatusCode.InternalServerError, e.ToString()); return new HttpResponseException(err); } } 

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; 
+1
source

An error of 500 means that you have an internal error in your service. It can be everything inside.

For example, your reader.GetValue (1) returns null. So you will have a null error and your server will return 500.

0
source

Source: https://habr.com/ru/post/1232236/


All Articles