Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 420
Rohan West Avatar asked Jul 05 '12 08:07

Rohan West


People also ask

How do I enable custom errors in IIS?

You can add custom error messages to IIS by adding an <error> element to the <httpErrors> element in the Web. config file for your site, application, or URL.

How do I redirect a custom error page in IIS?

Within the text box, “File path,” type the URL of the custom error page, and then click “OK.” Note: Make sure when selecting this option that the path is a relative path. Once you hit “OK,” your custom 404 error page should be live and doing its job.


2 Answers

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.

like image 136
mthizon Avatar answered Nov 13 '22 02:11

mthizon


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
like image 34
George Filippakos Avatar answered Nov 13 '22 04:11

George Filippakos