Set the 404 page under mvc4.0 in asp.net, mvc4.0404

Source: Internet
Author: User

Set the 404 page under mvc4.0 in asp.net, mvc4.0404

When our page does not exist or an error occurs, we need to display the 404 page to ensure better user experience and SEO.

Then how does one display the 404 page in MVC?

Of course, this is not an incorrect address or link jump to the ingress after a page error occurs.

First, create an mvcproject. First, store a 404.html page in the root directory.

Environment: VS2010,. NET Framework 4.0, MCV4, IIS7, WinServer2008 R2 Ent

As follows:

Then configure the site in IIS. If we do not perform any 404 processing now, the following error will occur when accessing a non-existent path.

When this error occurs, we first think of setting it in IIS and Web. config. Here we use another method to implement 404.

Now that something goes wrong, how can we get the error? Of course, it is Application_Error in Global. asax (here I will not repeat the program pipeline lifecycle and other issues.

Since the page address cannot be changed and the 404 page content needs to be displayed, we can do it in Application_Error.

Add the following code to Global. asax:

Protected void Application_Error ()
{
String path404 = System. Web. HttpContext. Current. Server. MapPath ("/404.html ");
StreamReader sr = new StreamReader (path404, System. Text. Encoding. UTF8 );
String content = sr. ReadToEnd (). ToString (); // read 404.html page content
Sr. Close ();
HttpContext. Current. Response. StatusCode = 404; // set the Page Status
HttpContext. Current. Response. Write (content); // output the 404 page content
HttpContext. Current. Response. End ();
HttpContext. Current. Response. Clear ();
}

Compile and access again

OK, it is what we want, the address remains unchanged, the page shows 404 content, the status code is also 404.

At this time, we were very happy to upload the file to the server (I first put it in the company's intranet testing server winserver 2008 r2 + iis7), and accessed it After configuring the site in iis.

When we access an error address After configuring the IIS Site on the server, the following page is displayed:

Why does it jump to the default 404 page of IIS? We have handled it in Global. asax?

Let's go to IIS to check the 404 error page settings.

We found that in IIS, the path of the 404 error page is set by default. After opening the folder in drive C: \ inetpub \ custerr \ zh-CN, you will find that all pages with corresponding page status of I are in this folder.

Can we modify the path? In this case, we do not need to modify it, but also put our own 404.html in this directory. What should we do next? Or open the C: \ inetpub \ temp \ appPools directory on disk C (the configuration file of the application pool of all sites configured in IIS, you will find that the folder contains all the folders with the same site name as you configured in IIS. We will open the folder and there will be a folder with the same site name. config file. You may need to be an administrator. If you cannot open it, copy it to another location, such as the desktop. Then open the config file. You will find the following node configuration

In this case, modify the 404 node to <error statusCode = "404" prefixLanguageFilePath = "F: \ test404 \ test404" path = "404.html" responseMode = "File"/>. The absolute path of 404.html in the pathpattern project.

After modification, replace the original. config file. When you access the wrong address again, the following page appears:

In addition, you can manually create a zh-cnfolder in the root directory to copy the 404.html file in the root directory.

The correct 404 page will be displayed after the second access is generated. The error address remains the same, and the F12 viewing status is also 404.

At this point, the 404 page is configured.

 

Address: http://www.cnblogs.com/liujinxin/p/6077717.html

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