C# HTTP Error 413.1 - Request Entity Too Large - unable to gracefully handle this error

Anjali Agarwal 1,366 Reputation points
2021-10-08T19:26:45.313+00:00

HTTP Error 413.1 - Request Entity Too Large The page was not displayed because the request entity is too large

ZuvvH.png

This is a webforms app, and I have these set in web.config:

<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="200288000" />
    </requestFiltering>
</security>

<httpRuntime executionTimeout="5400" maxRequestLength="200288" />

Those two settings set the max file size upload limits. However, when I test with a larger file, I get the IIS error page and am unable to gracefully catch and handle this error.

I've tried setting customErrors to On or Custom, but when the error occurs, it doesn't hit either the default redirect or the custom redirect?

<customErrors mode="On" defaultRedirect="/custom_error.aspx">
  <error statusCode="500" redirect="/custom_error.aspx" />
  <error statusCode="404" redirect="/not_found.aspx" />
  <error statusCode="413" redirect="/UploadError.aspx" />
</customErrors>

I just want to show a friendly message when the upload exceeded the size instead of showing 413.1 Uploade size exceeded error.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,133 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,240 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce Zhang-MSFT 3,736 Reputation points
    2021-10-11T07:11:26.25+00:00

    Hi @Anjali Agarwal ,

    CustomErrors mode is for Yellow Screen of Death (YSOD), but the error page you show is not YSOD. So the application doesn't show a friendly page to user.

    By the way, set mode="RemoteOnly" is better than On. If possible, please set it like this.

    • On – If defaultRedirect is specified, they will see that content. Otherwise, the default error screen with fewer details.
    • Off – Detailed error details will be shown to the user. (the “yellow screen of death screen”)
    • RemoteOnly – Default value. Detailed errors only are shown to local users. Remote users receive custom error screens or fewer details.

    A friendly error is displayed when viewed by a remote user, and the developer can obtain detailed errors through local access. Avoid modifying the mode every time you debug.

    IIS has a Error Pages module to allow users custom error page to avoid showing detailed error pages which you post.
    139366-3.jpg

    You can add custom error page for 413.1 status code.
    139391-4.jpg

    It also allow you to set detailed error message for local access and custom error page for remote access.
    139373-5.jpg


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Bruce Zhang

    0 comments No comments