mvc4.0 404 Page setup in ASP.

Source: Internet
Author: User
Tags httpcontext

When our page does not exist or there is an error, we need to show 404 pages, to ensure a better user experience and better SEO.

So how do you show 404 pages in MVC?

Of course, this is not our visit to a wrong address or page error after the link to jump to the www.xxx.com/404.html page, but on the wrong page or link is not changed, the page content to show 404, and the page status is also required 404.

First we create an MVC project, since it is 404, we first need a 404.html page placed in the root directory.

Environment: Vs2010,.net Framework 4.0,mcv4,iis7,winserver2008 R2 Ent

As follows:

Then configure the site in IIS, if now we do not do any 404 processing, access to a non-existent path will appear the following error

When this error occurs, the first thing we think about is to go to IIS and Web. config to set it up. Here we use another method to achieve 404.

Now that we have made a mistake, how can we get to the wrong, of course global.asax (here I will not repeat the program pipeline life cycle and other issues) in the Application_Error.

Since the page address can not be changed, but also to show 404 page content, then we will be in the Application_Error to tamper with.

Add the following code to the 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;//Setting page status
HttpContext.Current.Response.Write (content);//output 404 page Content
HttpContext.Current.Response.End ();
HttpContext.Current.Response.Clear ();
}

Compile Access again

OK, is the effect we want, the address is unchanged, page display 404 content, status code is also 404.

At this time we are very happy upload to the server (I first put in the company intranet test Server Winserver R2+IIS7), and after the IIS configuration site access, the problem comes.

After we have configured the IIS site in the server again, accessing an error address will display the following page:

Why jump to the IIS default 404 page, we clearly in the Global.asax program processing AH?

Let's go to IIS and take a look at the 404 error page settings

We found that in IIS he set the path address of the 404 error page by default. This error address again C disk C:\inetpub\custerr\zh-CN, open folder you will find, I all the page status corresponding pages are in this folder.

So are we going to change the path? We don't have to change this here, and we don't have to put our custom 404.html in this directory. What do we do next? Or in the C-drive C:\inetpub\temp\appPools directory (which is the profile of the application pool for all the sites that we configured in IIS), you will see that in this folder there will be all folders that match the name of the site you configured in IIS, and we'll open it. There will be a. config file with the same name as the site. You might need to be an administrator, and if you can't, copy it somewhere else, like the desktop. Open the config file again at this time. You will find the following node configuration

This time we changed the 404 node to <error statuscode= "404" prefixlanguagefilepath= "F:\test404\test404" path= "404.html" Responsemode = "File"/>. Path is the absolute path where 404.html is located in your project.

After the modification is complete, replace the original. config file. Revisit the error address and the following page appears:

Or not, the next step is to manually create the Zh-cn folder in the root directory and copy the 404.html files from the root directory into it.

Generate re-access will show the correct 404 page, the error address is unchanged, F12 view status is also 404.

This is the 404 page configuration completed.

There is time I will update how to again. Configure the 404 page in Netcore.

Original address: http://www.cnblogs.com/liujinxin/p/6077717.html

ASP. mvc4.0 under 404 Page Setup

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.