您现在的位置是:网站首页> 编程资料编程资料
ASP.NET设置404页面返回302HTTP状态码的解决方法_实用技巧_
2023-05-24
362人已围观
简介 ASP.NET设置404页面返回302HTTP状态码的解决方法_实用技巧_
在配置文件中配置404页面如下:
访问网站时错误页面可正常显示,但HTTP状态码却是302,对SEO很不友好,按下列步骤修改使错误页面返回正确的利于SEO的404状态码:
1、在404.aspx中加入代码:
Response.Status = "404 Moved Permanently";
如果你没有做伪静态,或者没加脚本映射,以上完全没有问题,不必往下看了。如果做了伪静态,那么404页面返回的状态码仍然为302,请看第二步。
2、在 Global.asax 中加入下面的代码:
protected void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
this.FileNotFound_Error();
}
///
/// 404错误处理
///
private void FileNotFound_Error()
{
HttpException erroy = Server.GetLastError() as HttpException;
if (erroy != null && erroy.GetHttpCode() == 404)
{
Server.ClearError();
string path = "~/404.aspx";
Server.Transfer(path);
//Context.Handler = PageParser.GetCompiledPageInstance(path, Server.MapPath(path), Context);
}
}
至此,这个顽固的问题得以解决。
复制代码 代码如下:
访问网站时错误页面可正常显示,但HTTP状态码却是302,对SEO很不友好,按下列步骤修改使错误页面返回正确的利于SEO的404状态码:
1、在404.aspx中加入代码:
Response.Status = "404 Moved Permanently";
如果你没有做伪静态,或者没加脚本映射,以上完全没有问题,不必往下看了。如果做了伪静态,那么404页面返回的状态码仍然为302,请看第二步。
2、在 Global.asax 中加入下面的代码:
复制代码 代码如下:
protected void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
this.FileNotFound_Error();
}
///
/// 404错误处理
///
private void FileNotFound_Error()
{
HttpException erroy = Server.GetLastError() as HttpException;
if (erroy != null && erroy.GetHttpCode() == 404)
{
Server.ClearError();
string path = "~/404.aspx";
Server.Transfer(path);
//Context.Handler = PageParser.GetCompiledPageInstance(path, Server.MapPath(path), Context);
}
}
至此,这个顽固的问题得以解决。
您可能感兴趣的文章:
- Nginx下301重定向域名的方法小结
- Apache Rewrite实现URL的301跳转和域名跳转
- 比较详细的win2003 IIS6.0 301重定向带参数的问题解决方法
- IIS7.5下301重定向的设置方法(及伪静态后301重定向出错案例)
- php 301转向实现代码
- iis下设置301 Redirect 永久重定向的方法
- php 实现301重定向跳转实例代码
- HTTP 错误 500.19- Internal Server Error 错误解决方法
- nginx提示:500 Internal Server Error错误的解决方法
- HTTP错误500.19解决方法(定义了重复的节点)
- http状态码汇总及问题经验总结
- javascript学习笔记(七)Ajax和Http状态码
- http状态码一览表
- PHP获取访问页面HTTP状态码的实现代码
- 前端获取http状态码400的返回值实例
- 详解HTTP状态码
- 10个常见的HTTP状态码详解
相关内容
- asp.net gridview列宽固定的几种方法介绍_实用技巧_
- asp.net中提示该行已属于另一个表的解决方法_实用技巧_
- asp.net 生成随机密码的具体代码_实用技巧_
- asp.net 按指定模板导出word,pdf实例代码_实用技巧_
- UpdatePanel和jQuery不兼容 局部刷新jquery失效_实用技巧_
- IE下document.referrer 拒绝访问的解决方法_实用技巧_
- GridView自定义删除操作的具体方法_实用技巧_
- asp.net Textbox服务器控件_实用技巧_
- SQLServer 在Visual Studio的2种连接方法_实用技巧_
- GridView使用CommandField删除列实现删除时提示确认框_实用技巧_
