title: ASP.NET MVC, HandleError Attribute, and Tracing: Bug?
body: <p>I seem to be encountering some funky behaviour when tracing is enabled in an ASP.NET MVC application:</p>
  
  <p>Whenever tracing is enabled, the HandleError attribute fails.</p>
  
  <p>I've reproduced this on the vanilla ASP.NET MVC app, and was wondering whether anyone has experienced anything similar.</p>
  
  <h1>Steps to Reproduce</h1>
  
  <h2>Step 1</h2>
  
  <p>Create a new ASP.NET MVC application</p>
  
  <h2>Step 2</h2>
  
  <p>Turn on tracing in <code>web.config</code>:</p>
  
  <pre><code>&lt;trace enabled="true" localOnly="false" pageOutput="false" requestLimit="500" traceMode="SortByTime" /&gt;
  </code></pre>
  
  <h2>Step 3</h2>
  
  <p>At this point everything works fine. The home page loads:</p>
  
  <pre><code>http://localhost/MvcApplication2/
  </code></pre>
  
  <p>and the trace page works:</p>
  
  <pre><code>http://localhost/mvcapplication2/trace.axd
  </code></pre>
  
  <h2>Step 4</h2>
  
  <p>Simulate an exception somewhere where the <code>HandleError</code> attribute can find it (controller action, view). </p>
  
  <p>I'm throwing an exception in the <code>Home\Index.aspx</code> view:</p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&gt;
  &lt;%@ Import Namespace="System.Threading"%&gt;
  
  &lt;asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server"&gt;
      Home Page
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;h2&gt;&lt;%= Html.Encode(ViewData["Message"]) %&gt;&lt;/h2&gt;
      &lt;p&gt;
          To learn more about ASP.NET MVC visit &lt;a href="http://asp.net/mvc" title="ASP.NET MVC Website"&gt;http://asp.net/mvc&lt;/a&gt;.
          &lt;% throw new NotImplementedException(); %&gt;
      &lt;/p&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <h1>Result</h1>
  
  <p>Instead of getting the a view returned by the <code>HandleError</code> filter (<code>Shared\Error.aspx</code>), I'm getting the ASP.NET CustomErrors error instead:</p>
  
  <pre><code>http://localhost/mvcapplication2/GenericErrorPage.htm?aspxerrorpath=/MvcApplication2/
  </code></pre>
  
  <h1>Digging Deeper</h1>
  
  <h2>Step 5</h2>
  
  <p>Disable tracing in <code>web.config</code>:</p>
  
  <pre><code>&lt;!--&lt;trace enabled="true" localOnly="false" pageOutput="false" requestLimit="500" traceMode="SortByTime" /&gt;--&gt;
  </code></pre>
  
  <h2>Result</h2>
  
  <p>The <code>HandleError</code> filter view (<code>Shared\Error.aspx</code>) is returned correctly:</p>
  
  <blockquote>
    <p>Sorry, an error occurred while
    processing your request.</p>
  </blockquote>
  
  <h1>Observations</h1>
  
  <p>Going back to Step 4 and doing some debugging reveals:</p>
  
  <ul>
  <li>The <code>HandleError</code> is being invoked correctly</li>
  <li>The <code>HandleError</code> view (<code>Shared\Error.aspx</code>) is throwing an error, and that's why we're sent to the ASP.NET custom errors</li>
  </ul>
  
  <p>Thanks, to ELMAH, here's the exception that the view is throwing:</p>
  
  <pre><code>System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---&gt; System.Web.HttpException: Multiple controls with the same ID 'ctl00' were found. Trace requires that controls have unique IDs.
     at System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize, Int32 controlStateSize)
     at System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState)
     at System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState)
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     --- End of inner exception stack trace ---
     at System.Web.UI.Page.HandleError(Exception e)
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     at System.Web.UI.Page.ProcessRequest()
     at System.Web.UI.Page.ProcessRequest(HttpContext context)
     at ASP.views_error_internalerror_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\c3a94d6e\b487cfcc\App_Web_m5awwxof.0.cs:line 0
     at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
     at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
     at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
     at System.Web.Mvc.Controller.ExecuteCore()
     at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>(Note that for this trace, my <code>HandleError</code> view is called "InternalError.aspx" instead of the default "Error.aspx")</p>
  
  <h1>Bug?</h1>
  
  <p>So the question is, do I get a cookie for finding a bug, or do I get slapped with a trout for missing something obvious?</p>
  
  <p>Thank you in advance for you help!</p>