Custom error page for Http error 404.13 ASP.NET Core MVC

Old post, but still relevant. My Core 2.2 MVC project, which include large streaming file uploads, needed a graceful handling of a 404.13 (request size too large) result. The usual way of setting up status code handling (graceful views) is in Startup.cs Configure() plus an action method to match:

app.UseStatusCodePagesWithReExecute("/Error/Error", "?statusCode={0}");

and

public IActionResult Error(int? statusCode = null)
{
    if (statusCode.HasValue)
    {
        Log.Error($"Error statusCode: {statusCode}");
        if (statusCode == 403)
        {
            return View(nameof(AccessDenied));
        }
        if (statusCode == 404)
        {
            return View(nameof(PageNotFound));
        }
    }

    return View(new ErrorViewModel 
        {
            RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier 
        });
}

But because a 404.13 error is handled by IIS, not in the MVC pipeline, the code above did not allow establishing a graceful 'upload too large' error view. To do that, I had to hold my nose and add the following web.config to my Core 2.2 project. Note that removing the 404.13 also removed 404, so the ErrorController() code no longer handled 404s, hence the two custom error handlers below. Hope this helps someone!

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 201Mb -->
        <requestLimits maxAllowedContentLength="210763776" />
      </requestFiltering>
    </security>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="13" />
      <remove statusCode="404" />
      <error statusCode="404"
             subStatusCode="13"
             prefixLanguageFilePath=""
             path="/Error/UploadTooLarge"
             responseMode="Redirect" />
      <error statusCode="404"
             prefixLanguageFilePath=""
             path="/Error/PageNotFound"
             responseMode="Redirect" />
    </httpErrors>
  </system.webServer>
</configuration>

You are correct. IIS is nabbing the error before it gets into your pipeline. I would recommend adding the httpErrors module into your web.config and pointing it at a page on the site.

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="13" />
    <error statusCode="404"
           subStatusCode="13"
           prefixLanguageFilePath=""
           path="http://yourwebsite.com/path/to/page"
           responseMode="Redirect" />
  </httpErrors>
</system.webServer>