title: Where do I catch and handle maxAllowedContentLength exceeded in IIS7?
body: <p>I have an aspx page where I’m allowing a user to upload a file and I want to cap the max file upload size to be 10MB.  IIS7, .NET 3.5.  I have the following configured in my web.config file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;location path="foo.aspx"&gt;
      &lt;system.web&gt;
          &lt;!-- maxRequestLength: kbytes, executionTimeout:seconds --&gt;
          &lt;httpRuntime maxRequestLength="10240" executionTimeout="120" /&gt;
          &lt;authorization&gt;
              &lt;allow roles="customRole"/&gt;
              &lt;!-- Deny everyone else --&gt;
              &lt;deny users="*"/&gt;
          &lt;/authorization&gt;
      &lt;/system.web&gt;
      &lt;system.webServer&gt;
          &lt;security&gt;
              &lt;requestFiltering&gt;
                  &lt;!-- maxAllowedContentLength: bytes --&gt;
                  &lt;requestLimits maxAllowedContentLength="10240000"/&gt;
              &lt;/requestFiltering&gt;
          &lt;/security&gt;
          &lt;handlers accessPolicy="Read, Script"&gt;
              &lt;add name="foo" path="foo.aspx" verb="POST"
                 type="System.Web.UI.PageHandlerFactory"
                 preCondition="integratedMode" /&gt;
          &lt;/handlers&gt;       
      &lt;/system.webServer&gt;
  &lt;/location&gt;
  </code></pre>
  
  <p>I have a custom error handling module that implements <code>IHttpModule</code>. I’ve found that when <code>maxRequestLength</code> is exceeded, <code>HttpApplication.Error</code> does indeed get raised. However when I play with <code>maxAllowedContentLength</code>, the <code>HttpApplication.Error</code> event isn’t being raised and the user gets redirected to a 404.13 page.  I've attached with Visual Studio with first chance exceptions turned on nothing is being thrown.  </p>
  
  <p>My first thought is to check the header content length in an earlier event – are there recommendations/best practices of where I do this? PostLogRequest?  EndRequest?</p>