Custom 404 page in ASP. Mvc4

Source: Internet
Author: User

Original address:

There are certainly many ways to define the 404 method. Different ways of showing the form is not the same, the user experience is not the same. The following 22 types of

Method One:

1. Find node <system.web> enable 404 configuration in Web. config

<customerrors defaultredirect= "~/error" mode= "on" redirectmode= "Responseredirect" ><error redirect= "/Error "Statuscode=" 404 "/></customerrors>

2. Define a controllers Error (this follows you), as defined in the action

Public ActionResult Index () {response.status = ' 404 Not Found '; Response.statuscode = 404;return View ();}

This way by default is to add to your URL? aspxerrorpath=/eg:http://localhost/error??aspxerrorpath=/123456 not recommended for trial

Method Two:

Open Global.asax file definition error turn to 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");             }        }

Custom 404 page in ASP. Mvc4

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: 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.