asp.net mvc输出自定义404等错误页面,非302跳转
朋友问到一个问题,如何输出自定义错误页面,不使用302跳转。当前页面地址不能改变.
还要执行一些代码等,生成一些错误信息,方便用户提交反馈.
500错误,mvc框架已经有现成解决方法:
filters.Add(new HandleErrorAttribute());
404错误目前想到的解决方法:
先上代码 Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError() as HttpException;
if (ex == null)
return; var httpStatusCode = ex.GetHttpCode(); if (httpStatusCode == )
{
var httpContext = (sender as MvcApplication).Context; httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = ;
ServiceFocus.LogService.AddLog(ex); httpContext.Response.ContentType = "text/html; charset=utf-8";
var routeData = new RouteData();
routeData.Values["controller"] = "Sys";
routeData.Values["action"] = "NotFound";
var requestContext = new RequestContext(new HttpContextWrapper(httpContext), routeData);
var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, "Sys") as SysController;
//controller.ViewData.Model=model;
(controller as IController).Execute(requestContext);
ControllerBuilder.Current.GetControllerFactory().ReleaseController(controller);
}
controller代码:
public class CompressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(acceptEncoding))
{
acceptEncoding = acceptEncoding.ToLower();
var response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
}
[Compress]
public class SysController : Controller
{
//
// GET: /sys/ public ActionResult NotFound()
{
return View();
}
public ActionResult Error()
{
return View();
}
}
web.config
启用输出错误信息,否则iss在外网请求的时候不会看到自定义的输出的错误信息
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
目前有几个疑惑,没有深究:还望有网友知道能解惑一二,就不用去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请求也类似.
仅仅是猜测,还未验证,
更多:
asp.net mvc输出自定义404等错误页面,非302跳转的更多相关文章
- asp.net mvc输出自定义404等错误页面,非302跳转。
朋友问到一个问题,如何输出自定义错误页面,不使用302跳转.当前页面地址不能改变. 还要执行一些代码等,生成一些错误信息,方便用户提交反馈. 500错误,mvc框架已经有现成解决方法: filters ...
- Laravel5.4中自定义404等错误页面
1.在resources/views/下简历文件夹error,在error文件中建立"404.blade.php文件". <!DOCTYPE html PUBLIC &quo ...
- 转:【译】Asp.net MVC 利用自定义RouteHandler来防止图片盗链
[译]Asp.net MVC 利用自定义RouteHandler来防止图片盗链 你曾经注意过在你服务器请求日志中多了很多对图片资源的请求吗?这可能是有人在他们的网站中盗链了你的图片所致,这会占用你 ...
- 【转】Asp.net MVC 通过自定义ControllerFactory实现构造器注入(重写DefaultControllerFactory)
[转]Asp.net MVC 通过自定义ControllerFactory实现构造器注入 一.重写ControllerFactory的GetControllerInstance ControllerF ...
- apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...
- spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。
此处用的是jsp,控制层用的是ModelAndView, 具体解决方法如下: @RequestMapping(value = "notfound", method = Reques ...
- ASP.NET MVC下自定义错误页和展示错误页的几种方式
在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
随机推荐
- 试用Let's encrypt
终于等到 https://letsencrypt.org beta了,马上下载试用,发现过程超简单. 1.首先需要下载letsencrypt的客户端,官方给的介绍是 The Let’s Encrypt ...
- CC3的多列属性Multi-column
CC3的多列属性Multi-column 一直都很想了解这个属性,总是忘了.今天可以研究一下,回想起想了解它的原因,大概是觉得它很容易分开几列.可能会有很多好处和方便. 0 16-09-17 1 16 ...
- PHP 中数组函数 isset 效率比 array_key_exists 更高
PHP 中数组函数 isset 效率比 array_key_exists 更高 PHP 浏览:4194 2014年04月05日 isset 和 array_key_exists 函数都可以用来测试数组 ...
- ibatis 更改resultmap后 java.sql.SQLException: Column 'del_status' not found.
当在resultmap中增加字段后,查询语句也必须增加相应字段,否则会报错, java.sql.SQLException: Column 'del_status' not found. 因为查询结果与 ...
- hdu 5363Key Set
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363 Problem Description soda has a set S with n inte ...
- man手册导出成txt,pdf,html的一些小技巧
经常man一些shell命令,有时候有想导出来编辑或注释一下,所以要导出.方法有很多种,根据自己的实际需要觉得比较实用的记录下分享一下. 1.导出成txt man –t bash |col –b &g ...
- KMP算法总♂结
讲KM♂P算法之前,我们先讲一个故♂事. 有一天,sgg给了老obo一封信和一个单词,并给他一个任务:找出这封信出现了多少个单词,然后在规定时间内告诉他. 碰到这个问题,老obo会怎么做呢? 首先最直 ...
- kibana 统计每天注册数
- WEB打印插件Lodop
Lodop.C-Lodop使用说明及样例 Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又可用程序代码直接实现 复杂打印.控件功能强大,却简单易用,所有调用如 ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...