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

时间:2022-11-29 17:38:27

I am checking for a valid page in my Global.asax file like this:

我正在检查我的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

And I have modified by web.config to include the line:

我已经通过web.config进行了修改以包含该行:

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

This works fine when running under Visual Studio Development Server or UltiDev Web Server, but when I use Local IIS Web Server in Visual Studio or full blown IIS and try to get to the nonexistant foo.php I get the standard IIS 404 error page. Note that if I navigate to foo.aspx I get my custom error page.

这在Visual Studio开发服务器或UltiDev Web服务器下运行时工作正常,但是当我在Visual Studio中使用本地IIS Web服务器或完全成熟的IIS并尝试访问不存在的foo.php时,我得到标准的IIS 404错误页面。请注意,如果我导航到foo.aspx,我会收到自定义错误页面。

It seems as if IIS is seeing the request before it gets to my code and deciding to reroute it? Is there any way round this?

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

1 个解决方案

#1


1  

You are just handling 404 errors for asp.net, php, png and all other non-dot-net files are not affected by this.

您只是处理asp.net,php,png的404错误,所有其他非dot-net文件不受此影响。

You need to set 404 pages on the IIS level to handle all invalid requests including ones for asp.net.

您需要在IIS级别设置404页面来处理所有无效请求,包括asp.net的请求。

for example in your web.config add:

例如在你的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>

#1


1  

You are just handling 404 errors for asp.net, php, png and all other non-dot-net files are not affected by this.

您只是处理asp.net,php,png的404错误,所有其他非dot-net文件不受此影响。

You need to set 404 pages on the IIS level to handle all invalid requests including ones for asp.net.

您需要在IIS级别设置404页面来处理所有无效请求,包括asp.net的请求。

for example in your web.config add:

例如在你的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>