How can I provide the HandleErrorInfo model to an object?

I am writing an ASP.NET MVC application and I ran into the following problem. I want to create custom error pages with detailed error information (for example, an error message or which controller caused the error), but I cannot provide the model in the view with the HandleErrorInfo object.

First, I configured the Web.config file to handle user errors:

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

Then I created an error controller to manage the error views:

 public class ErrorController : Controller { public ActionResult Index() { return View("Error"); } public ViewResult NotFound() { Response.StatusCode = 404; return View("NotFound"); } } 

And in the view, I added the model type:

 @model System.Web.Mvc.HandleErrorInfo 

But the model is null every time, and I don’t know how to pass the correct object with error data to the view and where this object should be created. Can you help me? Is it possible to do so?

+7
asp.net-mvc error-handling
source share
3 answers

So you are dealing with two different layers. <customErrors> is different from the basic ASP.NET framework. Then HandleError is different from MVC structure. When you use <customErrors> , ASP.NET does not know how to pass the exception information to the view, it will just load / redirect you to the error page.

To do a job review, you need to save everything inside the MVC ecosystem. Therefore you need to use HandleErrorAttribute or varient. You can see in the source code of the attribute , it is trying to detect when an unhandled exception has occurred, and if custom errors are included, then it will launch a new ViewResult with an instance of HandleErrorInfo that contains information about the exception inside it.

It should be noted that in the MVC project, by default, HandleErrorAttribute is used as a global filter inside the file App_Start/FilterConfig.cs .

+12
source share

I found another way. Sending error model for viewing from the controller.

1. Global.asax

 protected void Application_Error(object sender, EventArgs e) { var httpContext = ((MvcApplication)sender).Context; var currentController = " "; var currentAction = " "; var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); if (currentRouteData != null) { if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) { currentController = currentRouteData.Values["controller"].ToString(); } if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) { currentAction = currentRouteData.Values["action"].ToString(); } } var ex = Server.GetLastError(); //var controller = new ErrorController(); var routeData = new RouteData(); var action = "GenericError"; if (ex is HttpException) { var httpEx = ex as HttpException; switch (httpEx.GetHttpCode()) { case 404: action = "NotFound"; break; // others if any } } httpContext.ClearError(); httpContext.Response.Clear(); httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500; httpContext.Response.TrySkipIisCustomErrors = true; routeData.Values["controller"] = "Error"; routeData.Values["action"] = action; routeData.Values["exception"] = new HandleErrorInfo(ex, currentController, currentAction); IController errormanagerController = new ErrorController(); HttpContextWrapper wrapper = new HttpContextWrapper(httpContext); var rc = new RequestContext(wrapper, routeData); errormanagerController.Execute(rc); } 

2. Error controller:

 public class ErrorController : Controller { // // GET: /Error/ public ActionResult Index() { return RedirectToAction("GenericError", new HandleErrorInfo(new HttpException(403, "Dont allow access the error pages"), "ErrorController", "Index")); } public ViewResult GenericError(HandleErrorInfo exception) { return View("Error", exception); } public ViewResult NotFound(HandleErrorInfo exception) { ViewBag.Title = "Page Not Found"; return View("Error", exception); } } 

3. View / Shared / Error.cshtml:

 @model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = ViewBag.Title ?? "Internal Server Error"; } <div class="list-header clearfix"> <span>Error : @ViewBag.Title</span> </div> <div class="list-sfs-holder"> <div class="alert alert-error"> An unexpected error has occurred. Please contact the system administrator. </div> @if (Model != null && HttpContext.Current.IsDebuggingEnabled) { <div> <p> <b>Exception:</b> @Model.Exception.Message<br /> <b>Controller:</b> @Model.ControllerName<br /> <b>Action:</b> @Model.ActionName </p> <div style="overflow: scroll"> <pre> @Model.Exception.StackTrace </pre> </div> </div> } </div> 

4. web.config:

 <system.web> <customErrors mode="Off" /> </system.web> 

Credits: https://thatsimpleidea.wordpress.com/2013/10/28/mvc-error-page-implementation/#comment-166

+5
source share

This did not work for me, but it inspired me that my Global.asax.Application_Start() did not register the FilterConfig class:

 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 

This, combined with Stephen V's answer, resolved my null model problem.

+1
source share

All Articles