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?