MVC custom error page is it really easy? _ Practical Tips

Source: Internet
Author: User
Tags error code html page

If you're having problems setting up the MVC custom error page, you're not the only one. Surprised that you're doing the right thing that doesn't work because part of the error is handled by the pipeline, and the other part is handled directly by IIS.

Usually (I expect this to be the case, on some other frame/server) We just need to configure a custom error page in one place, no matter where the error is raised. Just like this:

<customerrors mode= "On" >
  <error code= "404" path= "404.html"/> <error "code="
  500. html "/>

Custom 404 Error page

When a resource does not exist (including static and dynamic), we need to return a 404 state of the page, usually we need to provide some slightly friendlier information instead of generated default error page presented to our site visitors, may be to give some advice Why the resource might not exist or provide a Web site for which you want to search.

Here is a simple demo set as follows:

<! DOCTYPE html>

I created a new MVC 5 application that contained the VS-band standard template. If I run it to try to navigate to a nonexistent path e.g./foo/bar, you will get a standard 404 page containing the following information:,

Not too friendly, isn't it?

The error in this case was raised by MVC because it did not find the controller or action that matched the URL.

To customize the 404 error page, in the Web.config <system.web></system.web> configuration section:

<customerrors mode= "On" >
 <error statuscode= "404" redirect= "~/404.html"/>

Mode= "On" so that we can see the error page locally. In general you may only want to be rendered as mode= "RemoteOnly" when put into use.

Now if I navigate to/foo/bar again, I can see the error page I just defined.

However, as I expected, the URL path at this time is not/foo/bar redirect it to/404.html?aspxerrorpath=/foo/bar, and I check that the HTTP status code for the response is also 200 of the normal state.

This is very bad, the return of HTTP code 200 will not only cause misunderstanding, but also not conducive to SEO. In simple terms, if the resource for the specified path does not exist, it should return 404 if the resource is moved, it should be redirected to the new path.

To fix this problem we can change the default behavior redirect error page to return for overrides (rewrite the response).

<customerrors mode= "on" redirectmode= "Responserewrite" >
 <error statuscode= "404" redirect= "~/404.html" />

But it doesn't make much difference. Although the original URL address has not been redirected, still returns 200, in addition to displaying our custom error page as plain text.

It seems that we have to return to a page. If you thought you didn't have to go to the *.aspx page before, I'm afraid I let you down.

As a result, the URL and content type (text/html) are normal after the error page and corresponding web.config are changed to 404.aspx.

But 200 of the problems remain. Microsoft's official solution to the problem is to set the status code for the page. We add the following sections in 404.aspx:

<% Response.statuscode = 404%>

Now that we have the correct status code, URL and custom error page, is this the end of the story?


If we link to a static page path (e.g. foo.html) or a URL that does not match our routing configuration (e.g./foo/bar/foo/bar), we will see a standard IIS 404 error page.

The above situation bypasses the handled by IIS. Of course, if you return a httpnotfound in controller ation () you will get the same result--because MVC simply sets the status code and does not throw an error , but instead gave it to IIS.

In this case we need to set the IIS error page (IIS 7+ only). In the Web.config <system.webServer></system.webServer> configuration section:


Also set errormode= "Custom" for local testing. The normal situation is set to Errormode= "Detailedlocalonly".

Note that I use an HTML page instead of ASPX. Usually you should use a simple static file as the error page, so that the error page will display correctly even if the error occurs.

Now, if we navigate to a nonexistent static file path, we get a custom error page instead of the IIS default 404 page, and the remaining 200 problems are the same as before.

Fortunately, IIS actually provides a built-in solution to this, if you set responsemode = "File" IIS will return your custom error page without changing the original response header:
<error statuscode= "404" path= "404.html" responsemode= "File"/>

Custom 500 error page

Most of them are copied from the above workaround, adding a custom 500 error page. There are a few points to note here.

The standard MVC template has built-in Handleerrorattribute as a global filter. Captures any errors raised in the MVC pipeline and returns a custom error view that provides you with the custom error enabled in Web.config. It will look for ~/views/{controllername}/error.cshtml or ~/views/shared/error.cshtml.

If you use filters (filter), you need to update the existing custom error view, and you need to create (preferably in the Views/shared folder).

I don't see this. The filter has a property value that can be set, and any exceptions thrown in the MVC pipe are returned to the standard error configuration page, and you cannot use this filter here if you want to set those * *.

Add the following custom error page configuration:

<customerrors mode= "on" redirectmode= "Responserewrite" >
 <error statuscode= "404" redirect= "~/404.aspx" />
 <error statuscode= redirect= "~/500.aspx"/>

Similar to the 404.aspx created earlier:

<% Response.statuscode =%>
<! DOCTYPE html>

Unfortunately, this does not capture every exception in your application. A fairly common error--the validation of a request generated by, such as a dangerous URL path/foo/bar<script></script>, which actually produces a 404 response; So you can add a default error configuration:

<customerrors mode= "Off" redirectmode= "Responserewrite" defaultredirect= "~/500.aspx" >
 <error Statuscode= "404" redirect= "~/404.aspx"/> <error statuscode= "" redirect= "~/500.aspx"/> </

Finally, to capture the exception, we set the IIS custom server internal Error 500 error page:

<error statuscode= "path=" "500.html" responsemode= "File"/>


Create the following error page in your application root directory:

404.html-for IIS
500.html-for IIS

Confirm that you set the appropriate response status code within the ASPX page.

Discard MVC Handleerrorattribute global filters; configure custom errors:

<customerrors mode= "RemoteOnly" redirectmode= "Responserewrite" defaultredirect= "~/500.aspx" >
 <error Statuscode= "404" redirect= "~/404.aspx"/> <error statuscode= "" redirect= "~/500.aspx"/> </

To configure the IIS custom error page:


Original link:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.