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.  

这是我到目前为止所拥有的,但它没有抓住它。

全球.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 ======

我根据 Mike 的回答进行了更改,并将 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

这是我尝试运行上述测试时遇到的异常。

4

2 回答 2

0

Based on what your code says you are trying to do, what you want is this:

<customErrors mode="On" defaultRedirect="~/Error/">
</customErrors>
于 2013-03-14T17:18:14.057 回答
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();
  }
}
于 2013-03-16T04:29:51.807 回答