asp.net mvc输出自定义404等错误页面,非302跳转。

朋友问到一个问题,如何输出自定义错误页面,不使用302跳转。当前页面地址不能改变.

还要执行一些代码等,生成一些错误信息,方便用户提交反馈.

 

500错误,mvc框架已经有现成解决方法:

filters.Add(new HandleErrorAttribute());

404错误目前想到的解决方法:

先上代码 Global.asax:

asp.net mvc输出自定义404等错误页面,非302跳转。asp.net mvc输出自定义404等错误页面,非302跳转。
 1 protected void Application_Error(object sender, EventArgs e)
 2         {
 3             var ex = Server.GetLastError() as HttpException;
 4             if (ex == null)
 5                 return;
 6 
 7             var httpStatusCode = ex.GetHttpCode();
 8 
 9             if (httpStatusCode == 404)
10             {
11                 var httpContext = (sender as MvcApplication).Context;
12 
13                 httpContext.ClearError();
14                 httpContext.Response.Clear();
15                 httpContext.Response.StatusCode = 404;
16                 ServiceFocus.LogService.AddLog(ex);
17 
18                 httpContext.Response.ContentType = "text/html; charset=utf-8";
19                 var routeData = new RouteData();
20                 routeData.Values["controller"] = "Sys";
21                 routeData.Values["action"] = "NotFound";
22                 var requestContext = new RequestContext(new HttpContextWrapper(httpContext), routeData);
23                 var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, "Sys") as SysController;
24                 //controller.ViewData.Model=model;
25                 (controller as IController).Execute(requestContext);
26                 ControllerBuilder.Current.GetControllerFactory().ReleaseController(controller);
27             }
28         }
View Code
controller代码:
asp.net mvc输出自定义404等错误页面,非302跳转。asp.net mvc输出自定义404等错误页面,非302跳转。
 1 public class CompressAttribute : ActionFilterAttribute
 2     {
 3         public override void OnActionExecuting(ActionExecutingContext filterContext)
 4         {
 5             var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
 6             if (!string.IsNullOrEmpty(acceptEncoding))
 7             {
 8                 acceptEncoding = acceptEncoding.ToLower();
 9                 var response = filterContext.HttpContext.Response;
10                 if (acceptEncoding.Contains("gzip"))
11                 {
12                     response.AppendHeader("Content-encoding", "gzip");
13                     response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
14                 }
15                 else if (acceptEncoding.Contains("deflate"))
16                 {
17                     response.AppendHeader("Content-encoding", "deflate");
18                     response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
19                 }
20             }
21         }
22     }
23     [Compress]
24     public class SysController : Controller
25     {
26         //
27         // GET: /sys/
28 
29         public ActionResult NotFound()
30         {
31             return View();
32         }
33         public ActionResult Error()
34         {
35             return View();
36         }
37     }
View Code

web.config

<system.webServer>
    <httpErrors errorMode="Detailed" />

目前有几个疑惑,没有深究:还望有网友知道能解惑一二,就不用去google 扒源码了。

 

1.如果不加这行代码,默认输出的是:text/html; 浏览器直接输出内容,不做解析.

httpContext.Response.ContentType = "text/html; charset=utf-8";

2.iis不会使用gzip压缩,不管输出的404错误页面有多大,都不会自动压缩.所以使用下面这种替换方式.

  [Compress]
    public class SysController : Controller

猜测:

mvc 在action的Execute阶段后 还做了不少事情,比如上面提到的1,2点.正常200请求会执行默认的filter等阶段.

而当是404请求时,跳过了这些阶段.可能500请求也类似.

仅仅是猜测,还未验证,

上一篇:后生:请不要改变异常传递路径


下一篇:.Net Core Linux centos7行—.net core json 配置文件