zoukankan      html  css  js  c++  java
  • ASP.NET设置404页面返回302HTTP状态码的解决方法

    在配置文件中配置404页面如下: 

    .代码如下:

    <customErrors mode="On" defaultRedirect="404.aspx"> 
    <error statusCode="403" redirect="404.aspx" /> 
    <error statusCode="404" redirect="404.aspx" /> 
    <error statusCode="400" redirect="404.aspx" /> 
    </customErrors> 


    访问网站时错误页面可正常显示,但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(); 

    /// <summary> 
    /// 404错误处理 
    /// </summary> 
    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); 


    至此,这个顽固的问题得以解决。

  • 相关阅读:
    在Linux下删除文件及文件夹(rm)
    修改Linux文件权限
    文件分页显示(ls -al |more)
    linux的文件权限
    Linux中的重启(reboot)
    linux关机前同步数据(sync)
    hdu4990 Reading comprehension 矩阵快速幂
    hdu4965 Fast Matrix Calculation 矩阵快速幂
    hdu4847 Wow! Such Doge! KMP
    hdu4705 Y 树形DP
  • 原文地址:https://www.cnblogs.com/thinkingthigh/p/4217861.html
Copyright © 2011-2022 走看看