2011-10-04 128 views
5

你好我正在尝试做一个重定向如果响应是一个404,但它没有按预期工作,你可以专家看到这个问题?它还是到通用的404404自定义重定向

在我的Global.asax

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
     if (Response.Status == "404 Not Found") 
     { 
      string notFound = "~/custom_notfound.aspx"; 
      Response.Redirect(notFound); 
     } 

} 

UPDATE

试过到目前为止

(Response.Status == "404 Not Found") 
(Response.Status == "404") 
(Response.StatusCode == 404) 
+0

您的意思是把'的Response.Redirect(NOTFOUND);'? – Icarus

+0

PageNotFound是什么? –

+0

ahh对不起,当张贴错别字,但仍然有问题。谢谢 – user710502

回答

8

您还可以使用web.config中的customErrors部分 - as shown here

eg在system.web节,

<customErrors mode="On" defaultRedirect="/custom_error.aspx"> 
    <error statusCode="404" redirect="/custom_notfound.aspx" /> 
</customErrors> 
2

你可以添加到您的web.config中要做到这一点重定向,你不需要使用Application_BeginRequest来处理这个问题。

看到这个ServerFault question

如果您不能使用web.config,那么我会将您的启动页面设置为不存在的页面,在您的BeginRequest中放置一个断点,调试应用程序并查看请求以了解如何确定它是404.这将更容易确定最佳解决方案。

仔细研究一下,在HttpWebResponse类中使用了HttpStatusCode。因此,使用不同的应用程序覆盖来获取默认响应是有意义的,然后根据Enum检查它的状态。

+0

在任何情况下,出于某种原因,他们希望它在asax中完成,这将是在那里做的方式? – user710502

3

我不认为BeginRequest可能知道大约404错误。请尝试实施Application_Error。检查Server.GetLastError()是否是HttpException,如果是,请检查状态。

0

您还可以使用在web.config

<system.webServer> 
    <httpErrors errorMode="Custom" defaultResponseMode="File" > 
    <remove statusCode="404" /> 
    <remove statusCode="500" /> 
    <error statusCode="404" path="404.html" /> 
    <error statusCode="500" path="500.html" /> 
    </httpErrors> 
</system.webServer>