我正在尝试安装一个很好的全局异常处理机制来集中管理一般抛出的异常,以及 404、500、...错误;也就是说,不必针对不同的错误实现不同的错误页面/错误处理解决方案。
为此,我尝试实现此示例以从 Global.asax.cs 的 Application_Error 方法中调用 ErrorsController。
但是,当我运行代码时,我遇到了这个异常:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The view 'NotFound' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Errors/NotFound.aspx
~/Views/Errors/NotFound.ascx
~/Views/Shared/NotFound.aspx
~/Views/Shared/NotFound.ascx
/favicon.ico
Source=System.Web.Mvc
StackTrace:
at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at Cider.MvcApplication.Application_Error(Object sender, EventArgs e) in c:\Users\cornelius.kopp\Projects\SD Cider\Cider\Global.asax.cs:line 88
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.HttpApplication.RaiseOnError()
InnerException:
有趣的是,我使用 Razor 作为视图引擎,并且模板确实存在。我实现了一个HandleErrorsAttribute
名为 ErrorsController 的自定义,并通过它一切顺利。仅当从 Application_Error 方法调用(基本上是复制和粘贴的)代码时,它才会失败。
尝试在项目中创建 NotFound.aspx 视图产生了相同的异常,只是它仅将 favicon.ico 列为搜索位置。
将代码移动到 Application_EndRequest 中,如最初编写的那样,产生了相同的结果
对此的任何解释将不胜感激。