2010-10-11 84 views
2

在web.config中,您可以添加该代码来处理404错误:如何正确处理ASP.NET中的404错误?

<customErrors mode="Off" defaultRedirect="ErrorPage.aspx"> 
    <error statusCode="404" redirect="/Site-Map"/> 
</customErrors> 

这工作得很好,你可以通过这个网址看看http://www.fundraising.com.au/thisisnotapage

然而,根据该网站http://gsitecrawler.com/tools/Server-Status.aspx响应代码因为这个不存在的页面是200,而不是404。

虽然此页面http://www.nuenergy.com.au/thisisnotapage确实在http://gsitecrawler.com/tools/Server-Status.aspx处显示404并重定向到另一页面。

你如何在ASP.NET中实现更高版本? 我需要的反应是一个404和另一个页面出现。

回答

1

custom errors section employs the HttpHandler拦截你没有一个页面,然后redirects the response使服务器返回Site-Map,而不是事实,是一种可用的资源,因此200点的状态是正确的,因为它好像要求第一是为Site-Map地点。

您最终想要服务Site-Map的两个版本,一个是实际请求(200),另一个是自定义错误响应(404)。我会创建Site-Map页面的另一个版本,OnError-Site-Map,扩展页面类以继承它的功能,但是然后overriding the http response code,以便它提供您想要的404状态,但仅适用于该扩展版本。然后将其放入web.config中的自定义错误部分。

1

为了您的页面返回404种状态,下面的代码添加到您的Page_Load事件:

Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    Response.Status = "404 Not Found" 

End Sub