Is the custom error page of ASP. net mvc really simple ?, Asp. netmvc

Source: Internet
Author: User

Is the custom error page of ASP. net mvc really simple ?, Asp. netmvc

If you encounter problems when setting the asp.net mvc custom error page, you are not alone. I'm surprised that your practice is correct. Some errors are handled by the asp.net pipeline, while others are handled directly by iis.

Generally (I expect this is the case, in some other frameworks/servers) We only need to configure the custom error page in one place, regardless of the errors caused. Like this ︰

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

Custom Error 404 page

When a resource does not exist (including static and dynamic resources), we need to return a 404 page, we usually need to provide some friendly information to replace the error pages generated by asp.net/iisand present them to our website administrators. This can be a piece of advice on why the resource might not exist or provide the website to be searched.

The following is a simple example:

<!DOCTYPE html>

I have created a new ASP. NET MVC 5 application, which contains the standard templates provided by. If I run it and try to navigate to a nonexistent path e.g./foo/bar, a standard ASP. NET 404 page containing the following information will be obtained :,

 

Unfriendly, isn't it?

This error is caused by ASP. net mvc because it does not find the controller or action that matches the url.

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

<customErrors mode="On"> <error statusCode="404" redirect="~/404.html"/></customErrors> 

Mode = "On" so that we can see the error page locally. In general, you may only want to render the image when it is put into use and set it to mode = "RemoteOnly ".

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

However, as I expected, the url path does not redirect/foo/bar ASP. NET to/404.html? Aspxerrorpath =/foo/bar, And the HTTP status code of the response is also 200 of the normal status.

This is very bad. Returning http code 200 will not only cause misunderstanding, but is not conducive to SEO. Simply put, if the resource in the specified path does not exist, 404 should be returned. If the resource is moved, it should be redirected to the new path.

To fix this problem, we can change the default behavior redirection error page of ASP. NET to rewrite the response ).

<customErrors mode="On" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/404.html"/></customErrors>

However, this does not have much effect (this foreigner is really cool ). although the original Url address is not redirected, ASP. NET still returns 200. In addition, the custom error page is displayed as plain text.

It seems that we have to return an ASP. NET page. If you thought you didn't need to go to the *. aspx page, I'm afraid you're disappointed.

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

However, the 200 issue still exists. The Microsoft official team provides the corresponding solution-set the page status code. We Add the following section in 404. aspx:

<% Response. StatusCode = 404%>

Now we get the correct status code, url, and custom error page. Is that all done?

Error.

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

The above situation bypasses ASP. NET is processed by IIS. of course, if you return an HttpNotFound () in controller ation, you will also get the same result -- this is because MVC simply sets the status code and does not throw an error, but gives it to IIS.

In this case, we need to set the iis error page (only valid for IIS 7 +). In the web. config <system. webServer> </system. webServer> Configuration section:

Set errorMode = "Custom" for local testing. Normally, it is set to errorMode = "DetailedLocalOnly ".

Note that I use html pages instead of aspx. Generally, you should use a simple static file as the error page, so that even if ASP. NET has an error, the error page can still be displayed normally.

Now, if we navigate to a non-existent static file path, we will get a custom error page instead of the default 404 page of IIS, and the rest will be the same as the previous 200 Issue.

Fortunately, IIS actually provides a built-in solution to solve this problem. If you set responseMode = "File" IIS, your custom error page will be returned, without changing the original response header ︰
<Error statusCode = "404" path = "404.html" responseMode = "File"/>
Done.

Custom Error 500 page

Most of them simply copy the preceding solution and add a custom 500 error page. There are several points worth noting.

The handleerrorattri embedded in the standard ASP. net mvc template serves as a global filter. Capture any errors caused by ASP. net mvc pipelines and return a custom "error" view to enable custom errors in web. config. It will look ~ /Views/{controllerName}/error. cshtml or ~ /Views/shared/error. cshtml.

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

I didn't see any attribute values that can be set for this filter. Any exceptions caused by the MVC pipeline will be returned to the standard ASP. NET error configuration page, since you want to set those **, this filter is not used here.

Add the following custom error page Configuration:

<customErrors mode="On" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/404.aspx"/> <error statusCode="500" redirect="~/500.aspx"/></customErrors>

Similar to the 404. aspx created earlier:

<% Response.StatusCode = 500 %><!DOCTYPE html>

Unfortunately, this does not capture every exception in your application. A fairly common error-caused by ASP. NET requests, such as a dangerous url path/foo/bar <script> </script>, will actually generate a 404 response; therefore, you can add a default error Configuration:

<customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx"> <error statusCode="404" redirect="~/404.aspx"/> <error statusCode="500" redirect="~/500.aspx"/></customErrors>

Finally, to capture non-ASP. NET exceptions, we set the IIS custom server internal Error 500 page:

<Error statusCode = "500" path = "500.html" responseMode = "File"/>

Summary

Create the following error page in the root directory of your application:

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

Make sure that you have set the appropriate response status code on the ASPX page.

Abandon the MVC HandleErrorAttribute global filter; configure a custom ASP. NET error:

<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx"> <error statusCode="404" redirect="~/404.aspx"/> <error statusCode="500" redirect="~/500.aspx"/></customErrors>

Configure the IIS custom error page:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.