Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API An error has occurred

I am working on ASP.NET Web API and when I ran 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 am trying to determine if its code or if its the server connection.

This is the stored procedure I got an example:

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

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

The error is

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

like image 587
user979331 Avatar asked Sep 26 '22 15:09

user979331


1 Answers

Turn your Method into this:

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);
    }
}

Then you can get your Exception ToString when the Error Occured.

To Ensure you can see the Exception Message, you can use a Fiddler or PostMan To test your API.

.


Besides, I just try the way in the OP's question comment,

In my case I'm testing returning some Exception Object defined by myself,

originally when I call that GET Method through enter url to Google Chrome,

Chrome just give me a meaningless meesage An error has occured,

and after I add

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

at Global.asax.cs in the method protected void Application_Start(),

my Exception Object showed correctly.

.


An hour later I figure out that it might also could be done in WebApiConfig.cs in the method public static void Register(HttpConfiguration config) by

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
like image 161
yu yang Jian Avatar answered Oct 16 '22 19:10

yu yang Jian