Home > Web Front-end > H5 Tutorial > body text

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

奋力向前
Release: 2021-08-27 11:11:54
forward
2896 people have browsed it

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 <system.web> in web.config and click to enable404Configuration

<customErrors defaultRedirect="~/Error" mode="On" redirectMode="ResponseRedirect">
<error redirect="/Error" statusCode="404" />
</customErrors>
Copy after login

2. Define a controllersError (this is up to you), and define <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">public ActionResult Index() { Response.Status = &quot;404 Not Found&quot;; Response.StatusCode = 404; return View(); }</pre><div class="contentsignin">Copy after login</div></div>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");
    }
}
Copy after login

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!

Related labels:
source:chuchur.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template