title: Custom error page for Http error 404.13 ASP.NET Core MVC
body: <p>I am fairly new to ASP.NET and MVC in general; I have been migrating an ASP.NET MVC app to ASP.NET MVC Core. In the former framework I was able to handle an HttpException for the following infamous error: </p>
  
  <blockquote>
    <p>HTTP Error 404.13 - Not Found</p>
    
    <p>The request filtering module is configured to deny a request that
    exceeds the request content length.</p>
  </blockquote>
  
  <p>I am aware I can increase the maximum upload allowed length, which currently is 30MB by default, but my objective is to present the user with a friendly error page explaining what just happened, not increase the allowed limit.</p>
  
  <p>On ASP.NET I did this with the following code on my Global.asax :</p>
  
  <pre><code>private void Application_Error(object sender, EventArgs e)
  {
      var ex = Server.GetLastError();
      var httpException = ex as HttpException ?? ex.InnerException as HttpException;
      if (httpException == null) return;
  
      if (httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
      {
          //handle the error
          Response.Redirect("~/Error/UploadTooLarge"); //Redirect to my custom error page
  
      }
  }
  </code></pre>
  
  <p>I can't seem to find an alternative for this in Asp.Net Core after several hours of research.
  I believe I will need to plug in some Middleware in my Startup.cs Configure method to achieve a custom error page to handle the HttpException and redirect it but I am truly lost on this matter.</p>
  
  <p>I have managed to successfully use the following middleware for custom error pages for http errors such as 404 - Not Found or 403 - Forbiden by using the following on my configure method :</p>
  
  <pre><code>app.UseStatusCodePagesWithReExecute("/Error/StatusCode{0}");
  </code></pre>
  
  <p>Alongside a controller:</p>
  
  <pre><code>public class ErrorController : Controller
  {
      public IActionResult StatusCode404()
      {
          return View(viewName: "CustomNotFound"); 
      }
  
      public IActionResult StatusCode403()
      {
          return View("CustomForbiden");
      }
  }
  </code></pre>
  
  <p>And the corresponding views. However, the 404.13 error (upload too large) wont be handled by my current middleware. I believe IIS is presenting the error as it is not handled by the web app.</p>