ASP. NET MVC Custom error page

Source: Internet
Author: User
If you're having trouble setting up an ASP. NET MVC custom error page, you're not alone. It's not surprising that you're doing the right thing, and it doesn't work because part of the error is handled by the ASP. NET pipeline, and the other part is handled directly by IIS.

Typically (I expect this to be the case, on some other frameworks/servers) we just need to configure the custom error page in one place, no matter where the error is thrown. Just like this:

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

Customizing the 404 error page

When a resource does not exist (contains static and dynamic), we need to return a 404 State page, usually we need to provide some slightly friendlier information to replace the default error page generated by Asp.net/iis to render to our site visitors, may be to offer some advice Why the resource may not exist or provide a selection of sites to search for.

Here are just a few simple demo settings:

<! DOCTYPE html>

I created a new ASP. NET MVC 5 application that includes a standard template with vs comes with. If I run it and try to navigate to a nonexistent path e.g./foo/bar, you will get a standard ASP. NET 404 page with the following information:,

Not very friendly, is it?

The error in this case is caused by ASP. NET MVC because it does not find a controller or action that matches the URL.

In order to customize the 404 error page, in the <system.web></system.web> configuration section of the Web. config:

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

Mode= "On" so we can see the error page locally. In general, you may only want to render as mode= "RemoteOnly" when you put it 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 ASP. NET to redirect it to/404.html?aspxerrorpath=/foo/bar, and I check the HTTP status code of the response is also 200 of the normal state.

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

To fix this problem we can change the ASP. NET default behavior redirection error page for the override return (rewrite the response).

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

But that doesn't make much of a difference. Although the original URL address is not redirected, ASP. NET still returns 200, and also displays our custom error page as plain text.

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

Therefore, the URL and content type (text/html) are normal after you change the error page and the corresponding Web. config to 404.aspx.

But 200 of the problems still exist. Microsoft's official solution to this 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?

Wrong.

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 scenario bypasses the request that ASP. NET was processed by IIS. If you return a httpnotfound () in controller ation, you will get the same result-because MVC is simply setting the status code without throwing an error Instead of handing it over to IIS.

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


Also set errormode= "Custom" for local testing. The normal condition will be set to Errormode= "Detailedlocalonly".

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

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 issues are the same as before.

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

Customizing the 500 error page

Most do not copy the above solution, add a custom 500 error page. Here are a few noteworthy places to watch.

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

If you use a filter, you need to update the existing custom error view and not exist (preferably in the views/shared folder)

I didn't see this. The filter has a property value that can be set, and any exception thrown in the MVC pipeline will be returned to the standard ASP. NET Error configuration page, since you have to set those * * This filter is not available here.

Add the following custom error page configuration:

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

Similar to the 404.aspx created earlier:

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

Unfortunately, doing so does not capture every exception in your application. A fairly common mistake-validation of a request generated by ASP./foo/bar<script></script>, such as a dangerous URL path, actually generates 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 "/></customerrors>

Finally, to catch a non-ASP. NET exception, we set the IIS custom server internal Error 500 error page:

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

Summarize

Create the following error page in your application's root directory:

404.html-for IIS
404.aspx-for ASP.
500.html-for IIS
500.aspx-for ASP.

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

Discard the MVC Handleerrorattribute Global filter and configure the custom error for asp:

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

To configure the IIS custom error page:


The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.


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.