日期:2014-05-18  浏览次数:20445 次

[***100分****],如何实现访问网站不存在的页时,出现“你访问的页不存在,5秒后跳回主面的效果。”。
如题:
我初学,想实现,一些网站访问不存在的页面时,就会跳到一页,并提示“你访问

的页不存在,5秒后跳回主面的效果”,请问您帮助我实现这些功能。

------解决方案--------------------
跳到相应的页面可以设置Web.Config文件

可以使用web.config文件的 <customErrors> 部分,再 <system.web> 标记内配置应用程序的定制错误页面:
<customErrors
defaultRedirect= "url "
mode= "On|Off|RemoteOnly ">
</customErrors>
DefaultRedirect表示如果发生错误,默认的URL就重定向到浏览器。这样如果页面访问失败,应用程序就可以恢复,并把用户定向到其他的页面

除了将所有的错误重定向到一个公共页外,还可以将特定的错误页分配给特定的错误状态代码。 <customErrors> 配置节支持内部 <error> 标记,该标记使 HTTP 状态代码与自定义错误页关联。例如:
<configuration>
<system.web>
<customErrors mode= "RemoteOnly " defaultRedirect= "/genericerror.htm ">
<error statusCode= "500 " redirect= "/error/callsupport.htm "/>
<error statusCode= "404 " redirect= "/error/notfound.aspx "/>
<error statusCode= "403 " redirect= "/error/noaccess.aspx "/>
</customErrors>
</system.web>
</configuration>
下表描述了 <error> 标记的属性和值。
属性 描述
StatusCode 自定义错误页对应的 HTTP 错误状态代码。例如:403 已禁止、404 未找到、500 内部服务器错误。
Redirect 发生错误时客户端浏览器应重定向到的 URL。


如果想显示动态效果,可以用javascript的setTimeout实现
------解决方案--------------------
Web.Config中

<customErrors mode= "RemoteOnly " defaultRedirect= "/errorpages/GeneralError.htm ">

<error statusCode= "404 " redirect= "/errorpages/FileNotFound.htm " />
<error statusCode= "403 " redirect= "/errorpages/GeneralError.htm " />
<error statusCode= "500 " redirect= "/errorpages/GeneralError.htm " />

</system.web>
</configuration>

FileNotFound.htm,就是你打算没有找到页时,出现的页面。
FileNotFound.htm的代码也给你,code from CSDN
<script type= "text/javascript ">
<!--
var duration=2900;
var endTime = new Date().getTime() + duration + 100;
function interval()
{
var n=(endTime-new Date().getTime())/1000;
if(n <0) return;
document.getElementById( "timeout ").innerHTML = n.toFixed(3);
setTimeout(interval, 10);
}
window.onload=function()
{
setTimeout( "window.location.href= 'http://你的主页 ' ", duration);
interval();
}
//-->
</script>

<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> 发生错误 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
对不起!你所访问的页面出错! <br />
系统在 <span id= "timeout "> 3.000 </span> 秒后 将自动跳转到 <a href= "http://你的主

页 ">
网站首页 </a>
</div>
</form>
</body>
</html>