2015-09-25 85 views
5

我正在使用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(內部服務器錯誤)的狀態

+4

你打算告訴我們發生了什麼樣的錯誤,或者我們應該使用我們的集體精神力量嗎?:) – DavidG

+0

不錯的@DavidG – user979331

+2

你還沒有向我們提供服務器的錯誤。 500內部服務器錯誤意味着你需要弄清楚服務器端錯誤是什麼。 – mason

回答

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

然後當錯誤發生時你可以得到你的異常ToString。

要確保您能看到異常消息,您可以使用FiddlerPostMan來測試您的API。


此外,我只是嘗試在OP的問題意見的方式,

對我來說,我測試返回由自己定義一些異常對象,

最初當我打電話說GET方法通過輸入網址到谷歌Chrome,

鉻只要給我一個毫無意義的meesage An error has occured

後,我加

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 

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

我的異常對象正確顯示。


一個小時後,我弄清楚,它可能也可以在WebApiConfig.cs

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 
+1

'e.ToString'不會看我,因爲它甚至會編譯。 –

+1

'connection.Close();'對我來說似乎沒有必要,因爲你已經在'using'塊中有連接了。 –

0

你500錯誤意味着你有內部錯誤與您的服務做的方法public static void Register(HttpConfiguration config)。它可能是裏面的一切。

例如,您的reader.GetValue(1)返回null。所以,你將有空錯誤,你的服務器將返回500.