2013-02-09 105 views
2

我爲我的Web應用程序使用Visual Studio 2010和MVC 4。這是我的控制器代碼:ASP.NET MVC中的500內部錯誤Ajax

公衆的ActionResult myController的()

{ 
     if (Request.IsAjaxRequest()) 
     { 
      using (MyContainer context = new MyContainer()) 
      { 
       try 
       { 
        var result = Some Query; 

        return PartialView("_MyView", result); 
       } 
       catch (Exception ex) 
       { 

       } 
      } 
     } 
     if (User.Identity.IsAuthenticated) 
     { 
      return RedirectToAction("Index", "Home", new { area = "User" }); 
     } 
     else 
     { 
      return Redirect("/"); 
     } 
    } 

這種方法將成功完成,但我的AJAX容器沒有顯示任何東西。在Firebug提出了這個錯誤:

NetworkError: 500 Internal Server Error + http://localhost....?X-Requested-With=XMLHttpRequest

爲什麼會出現這種錯誤發生的呢?
我該如何解決這個問題?
在此先感謝!

+0

使用調試器。 – leppie 2013-02-09 08:56:51

+0

您可以執行一些基本的故障排除,例如檢查是否引發了任何異常而不是空白的catch子句。 – 2013-02-09 08:58:35

+0

在你的web配置文件中設置''併發布完整的錯誤信息消息你在螢火蟲 – nemesv 2013-02-09 08:58:41

回答

1

可能以任何方式查看500內部服務器錯誤消息,因爲服務器上的某些內容處理得不好。在你的情況下,正如你所讚揚的那樣,你的MyContainer類型沒有實現IDisposable接口,所以你不能在using(){ }塊中使用這種類型。當您使用類型的塊時,此類型必須實現IDIsposable,因爲當它結束時,.Net Framework將從堆和參考中刪除實例。我在你的代碼上做了一些改變而沒有使用block。看看:

public ActionResult ActionName() 
{ 
    if (Request.IsAjaxRequest()) 
    { 
     try 
     { 
      MyContainer context = new MyContainer(); 

      var result = Some Query; 
      return PartialView("_MyView", result);  
     } 
     catch (Exception ex) 
     { 
      // return some partial error that shows some message error 
      return PartialView("_Error"); 
     } 
    } 

    if (User.Identity.IsAuthenticated) 
    { 
     return RedirectToAction("Index", "Home", new { area = "User" }); 
    } 

    return Redirect("/"); 
}