8

I have a simple ASP.NET MVC 3 website hosted in IIS 7.0 and am having difficulties displaying a custom http error page for a 404.13 http status code.

I have the following configuration in my Web.Config

<system.web>
    <httpRuntime maxRequestLength="2048"/>
    <customErrors mode="Off"/> 
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
        <clear/>
        <error statusCode="404" subStatusCode="-1" path="/home/showerror" responseMode="ExecuteURL"  />
        <error statusCode="404" subStatusCode="13" path="/home/showerror" responseMode="ExecuteURL"  />
    </httpErrors>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="1048576"/>
        </requestFiltering>
    </security>
</system.webServer>

When I navigate to a page that doesn't exist my error page is rendered correctly. However if I upload a file greater than 1MB I am presented with an empty 404 response. The url is never executed. If I change the responseMode to Redirect then the user is redirected correctly.

Rohan West
  • 9,262
  • 3
  • 37
  • 64
  • possible duplicate of [Display custom error page when file upload exceeds allowed size in ASP.NET MVC](http://stackoverflow.com/questions/2759193/display-custom-error-page-when-file-upload-exceeds-allowed-size-in-asp-net-mvc) – Darin Dimitrov Jul 05 '12 at 08:32
  • 1
    @DarinDimitrov That question uses responseMode=Redirect, it is a different problem, its not a duplicate. – Rohan West Jul 05 '12 at 08:39
  • 1
    It uses redirect because ExecuteURL doesn't work with 404.13 as explained in the duplicate answer. – Darin Dimitrov Jul 05 '12 at 08:54
  • @DarinDimitrov in that post the answer talks about clearing the error in this Application_Error method, in my instance this method is never called when a 404.13 occurs – Rohan West Jul 05 '12 at 09:03
  • 3
    Please read carefully the duplicate, I quote: `For uploaded files with size between maxRequestLength and maxAllowedContentLength IIS7 will throw an HttpException with HttpCode 500 and message text "Maximum request length exceeded"...`. And then: `For uploaded files with size bigger than maxAllowedContentLength IIS7 will display a detailed error page with error code 404 and subStatusCode 13`. I guess you are falling into the second case, that's why Application_Error is never invoked. IIS kills the request if you exceed this limit much before it ever had any chance to hit your application. – Darin Dimitrov Jul 05 '12 at 09:05
  • The only way to prevent this is to set the `maxAllowedContentLength` to some very big value and then you will be able to intercept the error inside your application, otherwise IIS simply won't let the request through. – Darin Dimitrov Jul 05 '12 at 09:07
  • @DarinDimitrov Thats true, I see why the Application_Error isn't called because the request hasn't made it that far. I'm still a bit confused why it has trouble with ExecuteURL. So it kills the request and doesn't honour the ExecuteURL – Rohan West Jul 05 '12 at 09:09
  • You may take it as a limitation if you will. The exact technical reason for this limitation is probably something you should ask the IIS engineers/designers about. – Darin Dimitrov Jul 05 '12 at 09:11

2 Answers2

3

The custom error isn't displaying most likely because of the configuration system's lock feature. Try the following command to unlock it:

%windir%\System32\inetsrv\appcmd unlock config -section:system.webserver/httperrors

After a couple of days of playing around with RequestFilters and CustomError pages, it finally worked for me.

mthizon
  • 33
  • 8
1

I had the same issue with IIS 7.5 in Integrated mode.

Eventually I gave up trying to handle the error in the web.config method and moved the error detection and handling to the Application_EndRequest instead:

Add the following to your global.asax file:

If you don't have a global.asax (MVC) then you could add it the the page error instead.

Protected Sub Application_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim context As HttpContext = HttpContext.Current.ApplicationInstance.Context
    If Not IsNothing(context) Then

        If context.Response.StatusCode = 404 And
           context.Response.SubStatusCode = 13 Then

               context.Response.ClearHeaders()
               context.Server.Transfer("~/errors/404.13.aspx", False)
        End If

    End If
End Sub
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
  • This solution does not work for me in MVC. It throws "Error executing child request for ..." exception. If I try to use Response.Redirect, it throws "Maximum request length exceeded." exception. – Der_Meister Jan 19 '16 at 04:56