Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET代码中处理IIS 404错误_Asp.net_.net_Iis_Visual Studio 2012 - Fatal编程技术网

在ASP.NET代码中处理IIS 404错误

在ASP.NET代码中处理IIS 404错误,asp.net,.net,iis,visual-studio-2012,Asp.net,.net,Iis,Visual Studio 2012,我正在检查Global.asax文件中的有效页面,如下所示: Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) ' Fires at the beginning of each request Dim path As String = HttpContext.Current.Request.Path.ToUpper If Not (path.EndsWith(".ASP

我正在检查Global.asax文件中的有效页面,如下所示:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request

    Dim path As String = HttpContext.Current.Request.Path.ToUpper

    If Not (path.EndsWith(".ASPX") OrElse path.EndsWith(".PNG") OrElse path.EndsWith(".GIF") OrElse path.EndsWith(".MASTER") OrElse path.EndsWith(".JS") OrElse path.EndsWith(".CSS") OrElse path.EndsWith(".PDF")) Then
        'invalid page
        Response.Redirect("/ErrorPageNotFound.aspx?aspxerrorpath=" + HttpContext.Current.Request.Path)

    End If
End Sub
我已经通过web.config修改了以下内容:

<error statusCode="404" redirect="ErrorPageNotFound.aspx"/>

在Visual Studio Development Server或UltiDev Web Server下运行时,这很好,但是当我在Visual Studio中使用本地IIS Web服务器或完整的IIS并尝试访问不存在的
foo.php
时,我会看到标准的IIS 404错误页。注意,如果我导航到
foo.aspx
,我会得到我的自定义错误页面


似乎IIS在请求到达我的代码并决定重新路由之前看到了请求?有什么办法可以解决这个问题吗?

您只是在处理404个错误,而asp.net、php、png和所有其他非.net文件都不受此影响

您需要设置以处理所有无效请求,包括asp.net请求

例如,在web.config中添加:

<system.webServer>
  <httpErrors existingResponse="Auto" errorMode="Custom" defaultResponseMode="File">
    <remove statusCode="404" subStatusCode="-1"/>
    <error statusCode="404" path="404.html"/>
  </httpErrors>
</system.webServer>


IIS是否设置为将文件类型定向到.NET?我无法控制实际的IIS服务器,因为该站点位于其他位置,因此我确实需要一个解决方案,如果需要,可以在项目中执行possible@MattWilko-您不需要访问IIS服务器,只需覆盖您自己的web.config中的全局IIS错误页。我加了一个例子。您的主机可能会在您的web.config中阻止此设置,但默认情况下是可以的。谢谢,我在IIS Express上本地运行了此设置,但在主机服务器上没有。