Home > Article > Web Front-end > In-depth analysis of mvc4 custom 404 page in asp.net (share)
In the previous article "An article explaining the usage of ES6 proxy Proxy in JS (code sharing)", we learned about the usage of ES6 proxy Proxy in JS. The following article will help you understand the mvc4 custom 404 page in asp.net. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
#Of course there are many ways to define 404. Different methods present different forms, and the user experience is also different. There are two
1. Find the section 2dc15ec6bc814c3aa45b55d017848bed
in web.config
and click to enable404
Configuration
<customErrors defaultRedirect="~/Error" mode="On" redirectMode="ResponseRedirect"> <error redirect="/Error" statusCode="404" /> </customErrors>
2. Define a controllersError
(this is up to you), and define <pre class="brush:php;toolbar:false">public ActionResult Index()
{
Response.Status = "404 Not Found";
Response.StatusCode = 404;
return View();
}</pre>
this in
as follows This method defaults to adding ?aspxerrorpath=/
to your url
. For example: http://localhost/Error??aspxerrorpath=/123456
, so it is not recommended. Try
OpenGlobal.asax
File definition error redirection address(controller/action)
protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404) { Response.Redirect("/Error"); } }
Note: During development, we often use the
Response.Redirect
method in theApplication_Error
method inGlobal.asax
to jump to a custom error page, but sometimes (especially when the site is deployed to IIS) theResponse.Redirect
method used in theApplication_Error
method will fail. When an abnormal error occurs, the default error yellow pages will still be displayed.
The fundamental reason is that although we used the
Response.Redirect
method in theApplication_Error
method, when an exception error occurs in the systemAsp.Net
believes that the exception has not been handled, so it will not jump to the page pointed to byResponse.Redirect
in theApplication_Error
method, and will eventually jump to the default error Yellow Pages.
The solution to this problem is very simple to use
Response.Redirect
in theApplication_Error
method before callingThe Server.ClearError()
method tells the system that the abnormal error that occurred has been handled, so that if theResponse.Redirect
method is called again, the system will jump to the custom error page.
Recommended learning: asp.net video tutorial
The above is the detailed content of In-depth analysis of mvc4 custom 404 page in asp.net (share). For more information, please follow other related articles on the PHP Chinese website!