Rohan West
Rohan West

Reputation: 9298

IIS Configuration for 404.13 Custom Error

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.

Upvotes: 8

Views: 5068

Answers (2)

George Filippakos
George Filippakos

Reputation: 16569

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

Upvotes: 1

mthizon
mthizon

Reputation: 33

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.

Upvotes: 3

Related Questions