404.13自定义错误的IIS配置

时间:2012-07-05 08:11:24

标签: asp.net-mvc-3 iis-7

我在 IIS 7.0 中托管了一个简单的 ASP.NET MVC 3 网站,但在显示 404.13 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响应。网址永远不会被执行。如果我将 responseMode 更改为重定向,则会正确重定向用户。

2 个答案:

答案 0 :(得分:3)

由于配置系统的锁定功能,最有可能无法显示自定义错误。请尝试以下命令将其解锁:

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

在使用RequestFilters和CustomError页面玩了几天后,它终于为我工作了。

答案 1 :(得分: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