网络配置:
<customErrors mode="On" defaultRedirect="~/Error/">
<error statusCode="404" redirect="~/Error/NotFound/"/>
<error statusCode="403" redirect="~/Error/AccessDenied/"/>
<\customErrors>
过滤器配置.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
吗?我错过了什么吗?