从Application_Error内执行控制器操作会抛出InvalidOperationException(未找到视图)

时间:2012-10-30 16:57:48

标签: asp.net asp.net-mvc-3 error-handling

我正在尝试安装一个很好的全局异常处理机制,集中管理泛型抛出的异常,以及404,500,......错误;也就是说,无需为不同的错误实现不同的错误页面/错误处理解决方案。

为此,我尝试implement this example从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作为视图引擎,模板肯定存在。我实现了一个调用ErrorsController的自定义HandleErrorsAttribute,并且通过它可以顺利完成所有工作。只有当从Application_Error方法调用(基本上是复制和粘贴的)代码时它才会失败。

尝试在项目中创建NotFound.aspx视图产生了相同的异常,只是它只将favicon.ico列为搜索位置。

将代码移动到最初编写的Application_EndRequest中,产生了相同的结果

对此的任何解释都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

谷歌把我带到了an issue on Jetbrain's issue tracker。我的R#缓存显然已经被破坏了;清除它并重新启动Visual Studio有帮助。 “清除缓存”似乎是解决问题的一种常用方法。 :|

How to clean caches:
1. Menu: RESHARPER->Options->General->Clear caches
2. Restart Visual Studio