Go straight to the chase.
There is a NotFound action method in HomeController.
Public ActionResult NotFound () { return View ();}
View Code
The corresponding view
@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Utf-8" /> <Metaname= "Viewport"content= "Width=device-width" /> <title>Something went wrong.</title></Head><Body> <Divstyle= "Text-align:center;font-size:1.5em">Why is the injury always Me O (╥﹏╥) o</Div></Body></HTML>
View Code
The Application_Error method, defined in Global.asax.cs, gets the error in the method and determines whether it is a 404 error for a static resource, and if not, uses the custom error page to display
Private ReadOnly string[] Staticfileext =New string[] {". Axd",". ashx",". bmp",". CSS",". gif",". htm",". html",". ico",". JPEG",". jpg",". js",". PNG",". rar",". zip",". Woff",". TTF",". EoT",". svg"};
View Code
protected void Application_Error () { var Error = Server.GetLastError () as HttpException; if (error!= null && error. Gethttpcode () = = 404) { if (! Isstaticresource (Request)) { response.clear (); Server.ClearError (); Response.tryskipiiscustomerrors = true; IController controller = new HomeController (); var routedata = new Routedata (); ROUTEDATA.VALUES.ADD ("Controller", "Home"); ROUTEDATA.VALUES.ADD ("Action", "NotFound"); Controller. Execute (New RequestContext (New Httpcontextwrapper (Context), routedata));}}} private bool Isstaticresource (HttpRequest request) { string extension = virtualpathutility.getextension (request. Path); return staticfileext.contains (extension);}
View Code
When the current station accesses a page or resource that exists:
Display a custom error page when accessing non-static resources that do not exist
When accessing a static resource that does not exist:
MVC implementation Custom 404 error page