Complete solution for ASP 3 404 error code return 302 problem

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/mycloudke/article/details/9746333

404 Status Code:, meaning that when the user clicks on the page does not exist, improve the user experience, the search engine will abandon the index of the page.

302 Status Code: Search engine will continue to index, which will lead to a large number of different links to the same page, it will affect the search engine trust in the site.

http://www.cloudke.com.cn/with some parameters or nonexistent file names, then jumps to the custom error page, but the returned status is 302.

Workaround:

The first step:

Set the customerrors of Web. config to Off

[CSharp]View Plaincopy
    1. <customerrors mode="Off" >
    2. </customErrors>


Then handle the custom error message yourself and add the following code to the Global.asax.cs:

[CSharp]View Plaincopy
  1. protected void Application_Error (Object sender, EventArgs e)
  2. {
  3. if (! request.islocal)// If you are testing locally, remove it!
  4. {
  5. Exception LastError = Server.GetLastError ();
  6. if (lasterror! = null)
  7. {
  8. Response.statuscode = 404;
  9. Response.WriteFile ("~/content/htmltemplate/notfound.htm"); outputs the contents of the Notfound.htm page. You can remove all HTML tags and enter "error" Directly .
  10. Server.ClearError ();
  11. }
  12. }
  13. }


~/content/htmltemplate/notfound.htm files can only be static files, so that you do not go to the ASP. NET MVC router.

After this preparation, found that there is still a problem, local access 404 can go to the custom page, but remote access is not, you need to configure IIS

Open error page, edit page 404

This file path is a custom error page 404.

OK, let's Test it again:

The test results are normal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.