【问题标题】:IIS Configuration for 404.13 Custom Error404.13 自定义错误的 IIS 配置
【发布时间】:2012-07-05 14:51:01
【问题描述】:

我有一个简单的 ASP.NET MVC 3 网站托管在 IIS 7.0 中,但在显示 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 则用户被正确重定向。

【问题讨论】:

  • @DarinDimitrov 该问题使用 responseMode=Redirect,这是一个不同的问题,它不是重复的。
  • 它使用重定向,因为 ExecuteURL 不适用于 404.13,如重复答案中所述。
  • @DarinDimitrov 在那篇文章中,答案谈到了清除此 Application_Error 方法中的错误,在我的例子中,当发生 404.13 时,永远不会调用此方法
  • 请仔细阅读副本,我引用:For uploaded files with size between maxRequestLength and maxAllowedContentLength IIS7 will throw an HttpException with HttpCode 500 and message text "Maximum request length exceeded"...。然后:For uploaded files with size bigger than maxAllowedContentLength IIS7 will display a detailed error page with error code 404 and subStatusCode 13。我猜你陷入了第二种情况,这就是为什么 Application_Error 永远不会被调用。如果您在它有机会访问您的应用程序之前远远超出此限制,则 IIS 会终止该请求。

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


【解决方案1】:

自定义错误没有显示很可能是因为配置系统的锁定功能。尝试以下命令解锁:

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

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

【讨论】:

    【解决方案2】:

    我在集成模式下的 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
    

    【讨论】:

    • 这个解决方案在 MVC 中对我不起作用。它抛出“执行子请求时出错...”异常。如果我尝试使用 Response.Redirect,它会抛出“超出最大请求长度”。例外。
    猜你喜欢
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多