MVC makes 404 jump instances (not 302 and 200)


The main causes of 404 are as follows:

1. Browsers and crawlers: some browsers will request the favicon.ico of the website. If there is no such file in the root directory of your website, the browser will have a 404 log. Similarly, the search engine will request robots.txt. But this has little impact.

2. The user entered the wrong URL: some users accidentally added or deleted a character in the browser address bar, resulting in the server unable to find the requested path.

3. The address referenced by some websites is too old: a page has been deleted, but other websites still reference it. When others click, the server cannot find the requested path.

404 and SEO:

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

In order to give a user-friendly experience, I made 404 pages and put them in the application_ Error, and then response. Redicet() to the 404 page. At that time, because I didn’t understand SEO, I thought it was a 404 page. The results show that this is a 302 jump, so the status code of 404 becomes 302, and when the search engine spider requests, it returns 302, it will think that your web page is normal! The invalid links of the website have not been deleted by the search engine. Over time, the dead chain of the website will be punished by the search engine. Therefore, the correct 404 jump should return the friendly page to the user and return the 404 HTTP status code to the spider.

Solution of 404 jump in

The last article talked about three methods of customizing error pages in, so I won’t say more here!

The third type of httperrors, the IIS error page, is used for the following reasons:

  1. Application_ Error: it seems difficult to return 404 status code while jumping 404. Generally, 302 will be returned;
  2. CustomErrors: when bloggers try to use it, they fail. They are not good at learning, alas;
  3. Httperrors: when bloggers try to use it, they happen to solve it, so it’s it;

Here’s how to operate!

IIS error pages can be used in three cases:

1. Return static file   2. Return to the dynamic page   3.302 redirection.

Three of them are ignored directly.

1. Return static file

Using this method, you need to prepare a static HTML page and put it on the hard disk. When specifying the path, you must give the absolute path.

Writing in web.config

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

Prefixlanguagefilepath indicates the file directory of the client language, path indicates the path of the file relative to the client language directory, responsemode indicates the response type, and here is file

Actions in IIS

Choose one of the two

Because bloggers failed when trying to use files, they don’t say much here. If they say much, they are tears!

2. Return to the dynamic page

Writing in web.config

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

The difference from the above is responsemode = “executeurl”.

Actions in IIS

Choose one of the two

The blogger finally chose this method, but this method has many advantagesSeveral pits need attention

1. You cannot specify a static HTML file in the relative directory of the website, such as 404.html.

2. For the specified dynamic pages cshtm, ASPX, etc., the response code shall be 404.

If you don’t pay attention to the above two situations, the response code returned by your 404 is not 404 or 302, but 200.

Because this method returns a web page under the root directory of the website as a 404 page, and as long as the static page can be accessed, it must be 200. If dynamic cshtml or ASPX does not specify a response code, it is not surprising to return 200.

Therefore, the specific methods are as follows (take MVC as an example):

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

Build a controller errorpage, which can define various error pages. Here is only 404 pages.

Then build a not found view and write the 404 page more beautiful.


At this time, ask for a path that does not exist.

Perfect solution!

Demo download address:Click to enter

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.