Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。

在Global中写一个Application_Error捕捉错误路由并重定向到Not Found页面。这里是全局性抓取错误路由,此处还可以写由错误路由导致访问失败的日志记录。

 1   protected void Application_Error(object sender, EventArgs e)
 2         {
 3             var httpContext = ((MvcApplication)sender).Context;
 4             var currentController = "";
 5             var currentAction = "";
 6             var currentRouterData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
 7 
 8             if (currentRouterData != null)
 9             {
10                 if (currentRouterData.Values["controller"] != null && !string.IsNullOrEmpty(currentRouterData.Values["controller"].ToString()))
11                 {
12                     currentController = currentRouterData.Values["controller"].ToString();
13                 }
14 
15                 if (currentRouterData.Values["action"] != null && !string.IsNullOrEmpty(currentRouterData.Values["action"].ToString()))
16                 {
17                     currentAction = currentRouterData.Values["action"].ToString();
18                 }
19             }
20 
21             var ex = Server.GetLastError();
22             //record error log here.
23 
24             var controller = new ErrorController();
25             var routeData = new RouteData();
26             var action = "Index";
27 
28             if (ex is HttpException)
29             {
30                 var httpEx = ex as HttpException;
31 
32                 switch (httpEx.GetHttpCode())
33                 {
34                     case  404:
35                         action = "Not Found";
36                         Response.Redirect("/Error/index");
37                         break;
38                 }
39 
40                 httpContext.ClearError();
41                 httpContext.Response.Clear();
42                 httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
43                 httpContext.Response.TrySkipIisCustomErrors = true;
44 
45                 routeData.Values["controller"] = "Error";
46                 routeData.Values["action"] = action;
47             }
48         }

NotFound页面:

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <meta charset="utf-8">
 9     <meta name="robots" content="noindex,nofollow">
10     <meta name="viewport" content="width=device-width,maximum-scale=1,user-scalable=no,minimal-ui">
11     <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800&amp;subset=latin,latin-ext">
12     <link rel="stylesheet" type="text/css" href="https://cdn.travel.sygic.com/travel.sygic.com_lp/css/404.css?4aed45ea0fff817941fb48877a968cf6cc920152">
13     <title>
14         404 Not found
15     </title>
16 
17 </head>
18 <body>
19     <div class="stars"></div>
20 
21     <div class="sun-moon">
22         <div class="sun"></div>
23         <div class="moon"></div>
24     </div>
25 
26     <div id="js-hills" class="background hills"></div>
27     <div id="js-country" class="background country"></div>
28     <div id="js-foreground" class="background foreground"></div>
29 
30     <div class="error-content">
31         Sorry, that page never returned<br>
32         from a Consultant to the <a href="/go/region:377">Marykay</a>.
33     </div>
34 
35     <a href="https://www.marykay.com.cn/" class="button-home">Go home</a>
36 
37     <div class="code">
38         <span>4</span>
39         <span>0</span>
40         <span>4</span>
41     </div>
42 </body>
43 </html>

在地址栏输入错误的路由-》效果

404页面来源于:

错误的艺术!20个创意的404错误页面设计

http://www.cnblogs.com/lhb25/p/creative-examples-of-404-error-pages.html

原文地址:https://www.cnblogs.com/kejie/p/6693664.html