I have a similar problem like it is described here I would like to have a generalized error handling mechanism for the controllers, if an error occurs I would like to display the same view where the error occured with added errors to the ModelState. I have something like this:
public override void OnException(ExceptionContext filterContext)
{
filterContext.Controller.TempData["UnhandledException"] = filterContext.Exception;
filterContext.ExceptionHandled = true;
((Controller)filterContext.Controller).ModelState.AddModelError(
((BaseException)filterContext.Exception).Message,
((BaseException)filterContext.Exception).StackTrace
);
filterContext.Result = new ViewResult
{
ViewName = this.View,
TempData = filterContext.Controller.TempData,
ViewData = filterContext.Controller.ViewData,
};
}
But I am receving null reference exception in Index.cshtml ln. 3 The error message: An exception of type 'System.NullReferenceException' occurred in App_Web_dc1p1y1y.dll but was not handled in user code
the code from my index.cshtml:
@model MyProject.Web.Models.UserProfileViewModel
@{
ViewBag.Title = "User profile";
}
<section>
<div class="noPadding">
<div class="col-xs-12 noPadding">
<h2>My profile</h2>
</div> etc....
So I dont want to redirect to some Error view, but I would like to display the error on the samve view where it occured.
What am I doing wrong ?