Is the custom ASP. NETMVC error page really simple ?, Asp. netmvc custom
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 containing the following information will be obtained.ASP. NET 404 page :,
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 Error 404 page<System. web> </system. web> Configuration section:
<customErrors mode="On"> <error statusCode="404" redirect="~/404.html"/></customErrors>
mode="On"In this way, we can see the error page locally. In general, you may only want to render it when you are in use and set itmode="RemoteOnly"。
Now, if I navigate/Foo/bar to 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 and return (RewriteThe 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.*. Aspx pageI'm afraid it will disappoint you.
Therefore, after you change the error page and the corresponding web. config to 404. aspx, the url and content type (text/html) are normal.
But 200 of the problems still exist. The Microsoft official team provides the corresponding solution-set the page status code.404. Add the following parts to 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.htmlOr a URL that does not match our route configuration (e.g./foo/bar/foo/bar), We will see a standard IIS 404 error page.
The above situation bypasses ASP. NET and IIS processes the request. Of course, if you returnHttpNotFound () will also get the same result-this is because MVC is just a simple settingStatus code does not throw an error. Instead, it is handed over to IIS.
In this case, we need to set the iis error page (only valid for IIS 7 +). In web. config<System. webServer> </system. webServer> Configuration section:
Same settingserrorMode="Custom"For local testing. normally seterrorMode="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.views/sharedFolder)
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-Authentication of requests generated by ASP. NET, such as a dangerous url path/foo/bar<script></script>This actually generates 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
Link: http://benfoster.io/blog/aspnet-mvc-custom-error-pages