我有一个运行良好的剃须刀网站,但有时我会得到一个未处理的例外。我试图追踪它,但现在我想改变它,他们没有看到这个:
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 ======
我按照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
这是我尝试运行上述测试时遇到的异常。
答案 0 :(得分:0)
根据您的代码所说的尝试,您想要的是:
<customErrors mode="On" defaultRedirect="~/Error/">
</customErrors>
答案 1 :(得分: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();
}
}