2013-03-14 61 views
0

我有一個剃刀網站運行良好,但偶爾我會得到一個未處理的異常。我想追查,但現在我想改變它,他們沒有看到這一點:試圖設置抓住所有未處理的異常,但它不工作

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Exception: Test 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

這裏是我迄今爲止,但它並沒有抓住它。

的Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes) 
{  
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute("UnhandledExceptions", "Error", new { controller = "Error", action = "Index" }); 
    routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Account", action = "Logon", id = UrlParameter.Optional }); 
} 

我ErrorController.cs:

using System.Web.Mvc; 

namespace SuburbanCustPortal.Controllers 
{ 
    public class ErrorController : Controller 
    { 
    public ActionResult Index() 
    { 
     return View("Error"); 
    } 
    } 
} 

我的測試代碼來生成一個錯誤:

public ActionResult Test() 
{ 
    throw new Exception("Test"); 
} 

我這是我的web.config:

<customErrors mode="On" > 
    <error statusCode="403" redirect="~/Error"/> 
    <error statusCode="404" redirect="~/Error"/> 
    <error statusCode="500" redirect="~/Error"/> 
</customErrors> 

我錯過了什麼?

======編輯#1 ======

我做每邁克的回答的變化,改變了web.config中:

<customErrors mode="On" defaultRedirect="~/Error/"> 
</customErrors> 

我得到此消息:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Exception: Test 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[Exception: Test] 
    SuburbanCustPortal.Controllers.HomeController.Test() in D:\Suburban\s\Web Projects\WebPortal\SuburbanCustPortal\Controllers\HomeController.cs:133 
    lambda_method(Closure , ControllerBase , Object[]) +79 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 
    System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +826266 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +825488 
    System.Web.Mvc.Controller.ExecuteCore() +159 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375 

這是我在嘗試運行上述測試時得到的異常。

+0

您是否試圖捕捉異常,或者當您點擊一個錯誤頁面時重定向到特定的錯誤頁面。我猜測後者。 – 2013-03-14 17:15:52

+0

好吧......我試圖防止任何錯誤發生,無論情況如何。 – ErocM 2013-03-14 19:19:30

回答

0

根據你的代碼,說你正在嘗試做的,你想要的是這樣的:

<customErrors mode="On" defaultRedirect="~/Error/"> 
</customErrors> 
+0

我試過了,得到了同樣的結果。沒有錯誤捕獲,但它顯示在網頁上。 – ErocM 2013-03-14 19:18:57

+0

對不起,這正是我在我的網站上所做的,它的工作原理沒有問題來解決你正在嘗試解決的問題。看看這個其他的SO帖子,也許有一些東西給你:http://stackoverflow.com/questions/13845318/asp-net-mvc-4-exception-handling-not-working – 2013-03-14 19:26:41

0

捕捉任何未處理的異常,請使用以下構建您的Global.asax:

public class MvcApplication : System.Web.HttpApplication 
{ 
    public MvcApplication() 
    { 
    Error += MvcApplication_Error; 
    } 

    private void MvcApplication_Error(object sender, EventArgs e) 
    { 
    Exception exception = application.Server.GetLastError(); 
    ...process exception... 

    // clear the exception from the server 
    application.Server.ClearError(); 
    } 
}