If I start with a new MVC 5 project, in web.config setting customErrors mode="on" allows the shared view 'Error.cshtml' to show when I force (raise) an exception, but it only shows the following text...
Error.
An error occurred while processing your request.
How do I pass information to this view to display more relevant info, such as what error occurred? Can I use this view if I use the Global.asax method...
protected void Application_Error()
?
Override the filter:
// In your App_Start folder
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ErrorFilter());
filters.Add(new HandleErrorAttribute());
filters.Add(new SessionFilter());
}
}
// In your filters folder (create this)
public class ErrorFilter : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
{
System.Exception exception = filterContext.Exception;
string controller = filterContext.RouteData.Values["controller"].ToString();;
string action = filterContext.RouteData.Values["action"].ToString();
if (filterContext.ExceptionHandled)
{
return;
}
else
{
// Determine the return type of the action
string actionName = filterContext.RouteData.Values["action"].ToString();
Type controllerType = filterContext.Controller.GetType();
var method = controllerType.GetMethod(actionName);
var returnType = method.ReturnType;
// If the action that generated the exception returns JSON
if (returnType.Equals(typeof(JsonResult)))
{
filterContext.Result = new JsonResult()
{
Data = "DATA not returned"
};
}
// If the action that generated the exception returns a view
if (returnType.Equals(typeof(ActionResult))
|| (returnType).IsSubclassOf(typeof(ActionResult)))
{
filterContext.Result = new ViewResult
{
ViewName = "Error"
};
}
}
// Make sure that we mark the exception as handled
filterContext.ExceptionHandled = true;
}
}
Declare the model at the top of the 'error' view:
@model System.Web.Mvc.HandleErrorInfo
Then use on the page like so:
@if (Model != null)
{
<div>
@Model.Exception.Message
<br />
@Model.ControllerName
</div>
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With