ASP.NET Custom error page for 404 returns 302 for http status

后端 未结 5 937
陌清茗
陌清茗 2020-12-17 20:32

In my asp.net web site I have custom error pages defined as following in my web.config file.



        
相关标签:
5条回答
  • 2020-12-17 21:16

    The solution was even simpler.. Please check out my response over here: http://blog.hebbink.com/post/2010/12/14/NET-custom-404-error-page-returns-302-for-http-status.aspx

    0 讨论(0)
  • 2020-12-17 21:17

    In ASP.NET 4.0 you can use redirectMode="ResponseRewrite" to send nice error pages and the proper HTTP code.

    0 讨论(0)
  • 2020-12-17 21:21

    After Googling about this issue, it seems that this is the default behavior that Microsoft ASP.NET provides for the situation. This is very bad for SEO. A work around I found is to check whether the requested file exists in an HTTP handler (or global.asax file), or use:

    <customErrors mode="On" redirectMode="ResponseRewrite">
        <error statusCode="404" redirect="/FileNotFound.aspx" />
    </customErrors>
    

    If the requested file does not exist, then rewrite the request path to a file not found page (if using an HTTP handler or global.asax), clear the server errors on the 404 error page code behind, and add a 404 error header to the response manually rather than waiting for server to do so.

    Server.ClearError();
    Response.Status = "404 Not Found";
    Response.StatusCode = 404;
    
    0 讨论(0)
  • 2020-12-17 21:23

    If you add the following to your 404 page code behind. You will get the correct results -

    Page 404.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
    }
    
    0 讨论(0)
  • 2020-12-17 21:26

    As you probably already know the 302 response is used to advise the caller that the requested resource has been moved to a different location.

    When you see, in Fiddler, the 302 http code being returned is there also a 'location' declaration in the header? For example:

    HTTP/1.1 302 Found
    Location: http://www.yoursite.com/somewhere/blah.htm
    

    It seems that you may have 'something' on the webserver that is intercepting the 404 returns and replacing these with 302's. I know this isn't much to go on but I would suggest that you look at the IIS configuration for the site.

    0 讨论(0)
提交回复
热议问题