8

我在IIS 7.0中托管了一个简单的ASP.NET MVC 3网站,并且在显示404.13 http 状态代码的自定义 http 错误页面时遇到了困难。

我的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>

当我导航到一个不存在的页面时,我的错误页面会正确呈现。但是,如果我上传一个大于 1MB 的文件,我会看到一个空的 404 响应。该 url 永远不会被执行。如果我将responseMode更改为Redirect,则用户将被正确重定向。

4

2 回答 2

3

由于配置系统的锁定功能,很可能不会显示自定义错误。尝试以下命令解锁:

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

在玩了几天 RequestFilters 和 CustomError 页面之后,它终于对我有用了。

于 2012-08-10T22:47:02.773 回答
1

我在集成模式下使用 IIS 7.5 时遇到了同样的问题。

最终我放弃了尝试处理 web.config 方法中的错误,而是将错误检测和处理移至 Application_EndRequest:

将以下内容添加到您的 global.asax 文件中:

如果您没有 global.asax (MVC),那么您可以将其添加到页面错误中。

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
于 2013-02-13T16:08:39.520 回答