Home  >  Article  >  Web Front-end  >  In-depth analysis of mvc4 custom 404 page in asp.net (share)

In-depth analysis of mvc4 custom 404 page in asp.net (share)

奋力向前
奋力向前forward
2021-08-27 11:11:542779browse

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.

In-depth analysis of mvc4 custom 404 page in asp.net (share)

#Of course there are many ways to define 404. Different methods present different forms, and the user experience is also different. There are two

methods provided below. One

1. Find the section 2dc15ec6bc814c3aa45b55d017848bed in web.config and click to enable404Configuration

<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 = &quot;404 Not Found&quot;; Response.StatusCode = 404; return View(); }</pre>this in

action

as follows This method defaults to adding ?aspxerrorpath=/ to your url. For example: http://localhost/Error??aspxerrorpath=/123456, so it is not recommended. Try

Method 2:

OpenGlobal.asaxFile 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 the Application_Error method in Global.asax to jump to a custom error page, but sometimes (especially when the site is deployed to IIS) the Response.Redirect method used in the Application_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 the Application_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 by Response.Redirect in the Application_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 the Application_Error method before calling The Server.ClearError() method tells the system that the abnormal error that occurred has been handled, so that if the Response.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!

Statement:
This article is reproduced at:chuchur.com. If there is any infringement, please contact admin@php.cn delete