当我从控制器操作中抛出异常时,自定义错误处理结果与InvalidOperationException

时间:2012-11-29 10:26:20

标签: error-handling

的Web.config:

<customErrors mode="On" defaultRedirect="~/Error/">
    <error statusCode="404" redirect="~/Error/NotFound/"/>
    <error statusCode="403" redirect="~/Error/AccessDenied/"/>
<\customErrors>

FilterConfig.cs:

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new LtcAuthorizeAttribute());
}

我从控制器操作中抛出异常,以查看我的自定义错误视图中的异常:

public ActionResult Index(int id) 
{
    if (id == 69732645)
    {
        throw new Exception("This is just to test an Error.");
    }
    var repository = ClaimsHelper.GetRepository();
    var rfbEntity = repository.Single<RequestForBenefit>(x => x.RfbId == id);
    var rfbModel = Mapper.Map<RequestForBenefit, RequestForBenefitModel>(rfbEntity);
    ViewBag.EobList = Mapper.Map<ICollection<EpisodeOfBenefit>, ICollection<EpisodeOfBenefitModel>>(rfbModel.EpisodeOfBenefit);
    repository.Dispose();
    return View(rfbModel);
}

错误控制器和索引操作:

public class ErrorController : Controller 
{
    //
    // GET: /Error/
    public ActionResult Index()
    {
        return View("Error");
    }
}

当抛出异常时,会发生另一个异常,InvalidOperationException会显示在视图上,而不是我故意从我的Index操作中抛出的异常。

这是错误视图中的内容:

Controller: Error 
Action: Index 
Message: Object reference not set to an instance of an object. 

Controller: 
at ASP._Page_Views_Error_Error_cshtml.Execute() in c:\Workspace\LTCClaims\Branches\Active\LtcClaims-2013Q1\Univita.LtcClaims\Univita.LtcClaims\Views\Error\Error.cshtml:line 12 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.StartPage.RunPage() 
at System.Web.WebPages.StartPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) 
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) 
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.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) 

有人可以告诉我为什么在抛出OperationInvalidException之后发生了Exception?我错过了什么吗?

0 个答案:

没有答案