ASP.NET Web API发生错误

时间:2015-09-25 00:43:48

标签: c# asp.net sql-server asp.net-web-api

我正在使用ASP.NET Web API,当我运行此方法时,我收到此错误发生错误。

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

我正在尝试确定其代码或服务器连接是否正确。

这是我得到一个例子的存储过程:

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

我尝试从存储过程中获取结果的方式有问题吗?

错误是

  

无法加载资源:服务器响应状态为500(内部服务器错误)

2 个答案:

答案 0 :(得分:2)

将您的方法转换为:

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

然后,您可以在错误发生时获取您的异常ToString。

要确保您可以看到异常消息,您可以使用FiddlerPostMan来测试您的API。

此外,我只是在OP的问题评论中尝试一下,

在我的情况下,我正在测试返回我自己定义的一些异常对象,

最初当我通过输入网址来调用GET方法时,

Chrome只是给了我一个毫无意义的meesage An error has occured

并在我添加

之后
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

方法Global.asax.cs中的protected void Application_Start()

我的异常对象显示正确。

一小时后,我发现可能也可以通过方法WebApiConfig.cspublic static void Register(HttpConfiguration config)中完成

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

答案 1 :(得分:0)

您500错误表示您的服务存在内部错误。它可能就是里面的一切。

例如,您的reader.GetValue(1)返回null。因此,您将出现null错误,您的服务器将返回500.