2014-10-27 140 views
1

我目前正在研究MVC應用程序。我的問題是,我無法讓我的客戶端錯誤處理正常工作。MVC自定義錯誤處理問題

所以基本上會發生什麼是我拋出一個異常來測試這個控制器。然後應用程序應該將用戶重定向到〜/ Shared/Errors.cshtml,但由於某種原因,它不會這樣做。

這是代碼。

拋出異常:

throw new Exception("Something went wrong"); 

Webconfig文件:

<customErrors mode="RemoteOnly"/> 

ErrorController代碼:在Error.cshtml使用

public class ErrorController : Controller 
    { 
     // 
     // GET: /Error/ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

} 

代碼:

@model System.Web.Mvc.HandleErrorInfo 
@{ 
    Layout = "_Layout.cshtml"; 
    ViewBag.Title = "Error"; 
} 
<div class="list-header clearfix"> 
    <span>Error</span> 
</div> 
<div class="list-sfs-holder"> 
    <div class="alert alert-error"> 
     An unexpected error has occurred. Please contact the system administrator. 
    </div> 
    @if (Model != null && HttpContext.Current.IsDebuggingEnabled) 
    { 
     <div> 
      <p> 
       <b>Exception:</b> @Model.Exception.Message<br /> 
       <b>Controller:</b> @Model.ControllerName<br /> 
       <b>Action:</b> @Model.ActionName 
      </p> 
      <div style="overflow:scroll"> 
       <pre> 
        @Model.Exception.StackTrace 
       </pre> 
      </div> 
     </div> 
    } 
</div> 

在此先感謝您可以給予的任何幫助

+0

通過檢查它是否工作? – 2014-10-27 11:19:23

+0

請檢查此[文章](http://www.prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc)。 – 2014-10-27 11:26:34

+0

我最近已經回答了類似的問題,看看它是否有幫助 - http://stackoverflow.com/questions/26357340/asp-mvc-handleerror-attribute-doesnt-work/26359455#26359455 – SBirthare 2014-10-27 12:23:25

回答

-2

您應該返回這樣的視圖。

return View("~/Views/Shared/Errors.cshtml");