ASP. net mvc creates 404 jump instances (not 302 and 200), mvc302 and

Source: Internet
Author: User

ASP. net mvc creates 404 jump instances (not 302 and 200), mvc302 and

404 of the causes are as follows:

1. browser and crawler: crawler. But this has little impact.

2. incorrect URL entered by the user: some users accidentally add or delete a character in the address bar of the browser, so that the server cannot find the Request Path.

3. Some websites reference an old address: a page has been deleted, while other websites still reference it. When others click it, the server cannot find the Request Path.

404 and SEO:

Due to the revision of this website, there are a lot of invalid links, and I also submitted a dead link to Baidu, but after half a month I did not see Baidu delete those invalid links. Later, I used the webmaster tool to query the HTTP status of those links and found that 302 was returned, which is no wonder.

To give a user-friendly experience, I made a 404 page, captured 404 in Application_Error, and then Response. Redicet () to the 404 page. At that time, because I didn't understand SEO, I thought that I had made a 404 page. The result shows that a 302 jump is made, so that the status code of 404 is changed to 302. When the search engine Spider requests, 302 is returned, it will think that your webpage is normal! As a result, the website's invalid links are never deleted by the search engine. Over time, the website's dead links will be punished by the search engine. Therefore, the correct 404 jump should be to return the friendly page to the user, while returning the 404 HTTP status code to the Spider.

Solution for redirecting from ASP. NET to 404:

The last article introduced three methods for customizing error pages in ASP. NET!

The third httpErrors is the IIS error page, for the following reasons:

  1. Application_Error: it seems that it is difficult to perform a 404 jump and return a 404 status code. Generally, 302 is returned;
  2. CustomErrors;
  3. HttpErrors: It happened to be solved when the blogger tried it;

The following describes how to perform this operation!

The IIS error page can be used in three situations:

1. Return static files 2. Return dynamic page 3.302 redirection.

3 of them are ignored directly.

1. Return static files

To use this method, you need to prepare a static html page and put it in the hard disk. When specifying the path, you must provide an absolute path.

Web. config

  <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" prefixLanguageFilePath="" path="D:\ErrorPage\404.html" responseMode="File" />

Operations in IIS

Select either of the two.

The attempt by the blogger to use files fails, so I will not talk about it here, but it is a tear!

2. Return to the dynamic page

The difference is responseMode = "ExecuteURL ".

Web. config

  <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" prefixLanguageFilePath="" path="/ErrorPage/NotFound" responseMode="ExecuteURL" />
Operations in IIS

Select either of the two.

This method was last selected by the blogger, but there are several pitfalls to note!

1. The static html file in the relative directory of the website cannot be specified, such as 404.html.

2. Specify the cshtm, aspx, and other dynamic pages. The response code must be 404.

If you do not pay attention to the above two situations, then the response code returned by your 404 is neither 404 nor 302, but 200.

Because this method returns a webpage under the root directory of the website as the 404 page, and as long as the static page can be accessed, it must be 200. Dynamic cshtml or aspx returns 200 if no response code is specified.

The specific method is as follows (using ASP. net mvc as an example ):

public class ErrorPageController : Controller
  public ActionResult NotFound()
    Response.Status = "404 Not Found";
    Response.StatusCode = 404;
    return View();

Create a controller ErrorPage, which can define various error pages. Here is only the 404 page.

Create a NotFound view and write the 404 page beautifully.


At this time, request a path that does not exist.

Perfect solution!

Demo: Click to enter

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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: 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.