Reputation: 1733
I have implemented exception handling using below code.[Edited Start] I do not know why it is calling view twice. [Edited Done]
Basecontroller
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled = true;
if (filterContext.Exception.GetType() == typeof(ArgumentOutOfRangeException))
{
this.View("OutOfRange").ExecuteResult(this.ControllerContext);
}
else
{
this.View("Error").ExecuteResult(this.ControllerContext);
}
}
base.OnException(filterContext);
}
}
HomeController
public class HomeController : BaseController
{
public ActionResult Exception2()
{
throw (new ArgumentOutOfRangeException());
}
public ActionResult Exception3()
{
throw (new Exception());
}
}
Error View (Shared folder only)
@model System.Web.Mvc.HandleErrorInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>
Sorry, an error occurred while processing your request.
</h2>
<h3>
@if (Model != null)
{
<p>@Model.Exception.GetType().Name<br />
thrown in @Model.ControllerName @Model.ActionName</p>
<br />
@Model.Exception
}
</h3>
</body>
</html>
OutOfRange view (Shared folder only)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>OutOfRange Exception</title>
</head>
<body>
<div>
<h3>
@if (Model != null)
{
<p>@Model.Exception.GetType().Name<br />
thrown in @Model.ControllerName @Model.ActionName</p>
<br />
@Model.Exception
}
</h3>
</div>
</body>
</html>
Execution URL : http://[domainName]/Home/Exception2
Here it is working fine. [EDITED : Here it also call twice.]
Execution URL : http://[domainName]/Home/Exception3
Here it is not working fine. You can see that "Sorry, an error occurred while processing your request." is coming twice. When I debugged the application using above URL, Error view called twice ( First time Model is null and second time Model contains some value). May I know what is wrong with my implementation?
Upvotes: 3
Views: 3768
Reputation: 11
I had the same problem, but I managed to resolve the error by adding:
Response.End();
To the end of my OnException method.
I know it's not the ideal resolution, but, it did at least get me out of a bind until such a time as I can investigate a more complete option.
Upvotes: 1
Reputation: 1038720
Things to try:
HandleError
global action filter attribute from your Global.asax
if it is present. When you create a new ASP.NET MVC 3 application the default template registers it inside the RegisterGlobalFilters
method. So comment out the line which registers this attribute.Enable custom errors in your web.config:
<customErrors mode="On" />
UPDATE:
If you want to pass a model to the Error.cshtml
view (as it is strongly typed to HandleErrorInfo
) you could do the following:
else
{
string controllerName = filterContext.RouteData.GetRequiredString("controller");
string actionName = filterContext.RouteData.GetRequiredString("action");
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
var result = new ViewResult
{
ViewName = "Error",
ViewData = new ViewDataDictionary<HandleErrorInfo>(model)
};
filterContext.Result = result;
}
Upvotes: 7
Reputation: 3061
Add base.OnException(filterContext);
before
if (filterContext.HttpContext.IsCustomErrorEnabled)
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (filterContext.HttpContext.IsCustomErrorEnabled)
}
Upvotes: 1