【问题标题】:ASP.NET Web API An error has occurredASP.NET Web API 发生错误
【发布时间】:2017-04-06 14:49:48
【问题描述】:

我正在使用 ASP.NET Web API,当我运行此方法时,我收到此错误 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;
}

我试图确定是它的代码还是它的服务器连接。

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

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

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

错误是

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

【问题讨论】:

  • 你是要告诉我们发生了什么错误,还是我们应该使用我们的集体精神力量? :)
  • 您仍然没有向我们提供来自服务器的错误。 500 Internal Server Error 意味着您需要弄清楚服务器端错误是什么。
  • 我认为我无法访问该信息
  • 在 VS 中 CheckDevice() 方法的第一行放置一个断点。以某种方式(例如,从浏览器)点击端点,然后调试您的代码。没有人能够告诉您 500 错误的原因是什么 - 它在您的代码内部,只有您可以确定潜在的错误是什么。
  • 用一个简单的try-catch块捕获异常并检查细节怎么样?

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


【解决方案1】:

把你的方法变成这样:

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

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

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

.


另外,我只是尝试一下OP的问题评论中的方式,

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

最初当我通过输入 url 到谷歌浏览器调用该 GET 方法时,

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

在我添加之后

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

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

我的异常对象显示正确。

.


一个小时后,我发现它也可以在 WebApiConfig.cs 的方法 public static void Register(HttpConfiguration config) 中通过

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

【讨论】:

  • e.ToString 在我看来并不像它甚至会编译。
  • connection.Close(); 在我看来是不必要的,因为您已经在 using 块内建立了连接。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2019-05-12
    • 2016-05-10
    • 2021-05-25
    • 2017-12-04
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多