Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are ASP.NET MVC 3 exceptions being 'handled' twice?
    text
    copied!<p>I have implemented exception handling using below code.[Edited Start] I do not know why it is calling view twice. [Edited Done]</p> <p>Basecontroller</p> <pre><code>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); } } </code></pre> <p>HomeController</p> <pre><code>public class HomeController : BaseController { public ActionResult Exception2() { throw (new ArgumentOutOfRangeException()); } public ActionResult Exception3() { throw (new Exception()); } } </code></pre> <p>Error View (Shared folder only)</p> <pre><code>@model System.Web.Mvc.HandleErrorInfo @{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Error&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt; Sorry, an error occurred while processing your request. &lt;/h2&gt; &lt;h3&gt; @if (Model != null) { &lt;p&gt;@Model.Exception.GetType().Name&lt;br /&gt; thrown in @Model.ControllerName @Model.ActionName&lt;/p&gt; &lt;br /&gt; @Model.Exception } &lt;/h3&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>OutOfRange view (Shared folder only)</p> <pre><code>@{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;OutOfRange Exception&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;h3&gt; @if (Model != null) { &lt;p&gt;@Model.Exception.GetType().Name&lt;br /&gt; thrown in @Model.ControllerName @Model.ActionName&lt;/p&gt; &lt;br /&gt; @Model.Exception } &lt;/h3&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Execution URL : <code>http://[domainName]/Home/Exception2</code></p> <p>Here it is working fine. [EDITED : Here it also call twice.]</p> <p><img src="https://i.stack.imgur.com/tQ2dj.png" alt="enter image description here"></p> <p>Execution URL : <code>http://[domainName]/Home/Exception3</code></p> <p><strong>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?</strong> <img src="https://i.stack.imgur.com/ZVb2Z.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload