ASP. NET MVC implementation 404 Jump Code instance

Source: Internet
Author: User
Tags response code
This article mainly introduces the ASP 404 Jump Instance (not 302 and 200), has certain reference value, the interested small partners can refer to

The main reasons for 404 are as follows:

1. Browsers and crawlers: Some browsers will request the favicon.ico of the site, and if you do not have this file in the root directory of your website, then the browser will have a 404 log, the same search engine will request robots.txt. But this has little effect.

2. The user entered the error URL: Some users accidentally added a character to the browser address bar or deleted a character, causing the server to find the requested path.

3. Some websites refer to an old address: a page has been deleted, while other sites still refer to others, the server cannot find the requested path when clicked.

404 with SEO:

This site because of the revision, so there are a lot of invalid links, and I also submitted a dead chain to Baidu, but after half a month also did not see Baidu delete those invalid links. Later I use Webmaster tools to query the HTTP status of those links, found unexpectedly returned is 302, this is no wonder.

To give a user-friendly experience, I made 404 pages and captured 404 in Application_Error, then Response.redicet () to 404 pages. At that time because do not understand SEO, think this even made 404 pages. The results found that this is done a 302 jump, so that 404 of the status code becomes 302, and the search engine spider request, the return is 302 words, it will think you this page is normal! Cause the site's failure link has not been deleted by the search engine, over time, the site's dead chain, will be punished by the search engine. So, the correct 404 jump should be to return the friendly page to the user while returning 404 of the HTTP status code to the spider.

Asp. NET in the 404 Jump Solution:

The last article about the three ways to customize error pages in ASP, here is not much to say!

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

    1. Application_Error: It seems difficult to do 404 jump at the same time return 404 status Code, will generally return 302;

    2. CustomErrors: Bo Master tried to use the time, failed, the Apprentice is not fine ah, ah;

    3. HttpErrors: Bo Master tried to use the time, happened to solve, so it is;

Here's how it's done!

The use of IIS error pages is divided into three different scenarios:

1. Return a static file 2. Returns dynamic page 3.302 redirection.

3 of them are ignored directly.

1. return static files

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

Syntax in Web. config


  <httpErrors errorMode="Custom" existingResponse="Replace">

  <remove statusCode="404" subStatusCode="-1" />

  <error statusCode="404" prefixLanguageFilePath="" path="D:\ErrorPage\404.html" responseMode="File" />



prefixLanguageFilePath represents the file directory of the client language, path represents the path of the file relative to the client language directory, responseMode represents the response type, here is File (file)

Operations in IIS
Choose one of the two

Since the bloggers tried to use the file and ended up failing, so I won’t say much here, it’s more tears!

2. Return to the dynamic page

Written in web.config


  <httpErrors errorMode="Custom" existingResponse="Replace">

  <remove statusCode="404" subStatusCode="-1" />

  <error statusCode="404" prefixLanguageFilePath="" path="/ErrorPage/NotFound" responseMode="ExecuteURL" />



Different from the above is responseMode="ExecuteURL".

Choose one of the two

The blogger finally chose this method, but this method has several pits to pay attention to!

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

2. The specified dynamic page cshtm, aspx, etc., need to specify a response code of 404.

If you don't pay attention to the above two situations, then your 404 returns a response code that is not 404 nor 302 but 200.

Because this method is to return a web page in the root directory of the website as a 404 page, and if the static page can be accessed, it is definitely 200. Dynamic cshtml or aspx If you do not specify a response code, then it is not surprising to return 200.

So the specific approach is as follows (take 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 just a 404 page.

Then build a NotFound view and write the 404 page a bit more beautifully.


At this time, go to the path that does not exist.
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.