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
<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请求也类似.
仅仅是猜测,还未验证,
asp.net mvc输出自定义404等错误页面,非302跳转。的更多相关文章
- asp.net mvc输出自定义404等错误页面,非302跳转
朋友问到一个问题,如何输出自定义错误页面,不使用302跳转.当前页面地址不能改变. 还要执行一些代码等,生成一些错误信息,方便用户提交反馈. 500错误,mvc框架已经有现成解决方法: filte ...
- 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:缓存的时间,以秒为 ...
随机推荐
- WPF捕获未处理的异常
WPF程序中,对于异常的捕获一般使用try/catch块.就像程序中的bug一样,很难保证程序中所有的异常都能够通过try/catch捕获.如果异常没有被捕获,轻则影响用户体验,严重时会导致数据丢失 ...
- KVM 介绍(4):I/O 设备直接分配和 SR-IOV [KVM PCI/PCIe Pass-Through SR-IOV]
学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...
- 大话设计模式C++版——工厂模式在COM中的典型应用
上篇<大话设计模式C++版——抽象工厂模式>中,我们拯救世界未遂,留下小小的遗憾,本篇中我们将给出一个解决方案——COM组件技术,同时也顺便扯扯工厂模式在COM组件技术中的应用. 工厂模式 ...
- css3 @font-face
很长时间,web设计师总是得用一些“web-safe”字体,英文用body{font-family:"corbel", Arial, Sans-serif; }中文用body{f ...
- 《100种过度医疗大公开》:转译自日文版,日文版依据的是美国的“Choosing Wisely”项目。三星推荐
本书转译自日文,日文版则是在美国的“Choosing Wisely”项目中选择了100个相对常见的过度医疗项目做解说.Choosing Wisely项目,是由美国多个专业医学组织发起的列出过度医疗项目 ...
- [转]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package
本文转自:http://www.mytecbits.com/microsoft/dot-net/bootstrap-3-0-0-with-asp-net-web-forms In my earlier ...
- CF687A. NP-Hard Problem[二分图判定]
A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- TC(Total Commander)文件管理神器
TC文件管理神器 Total Commander是一个会显著提高文件操作效率的工具,而文件操作是应用计算机最基本的功夫,也是伴随一生的操作.因此花一点时间学习,而会受益一世. Total Comman ...
- PagerHelper-分页类
2016.01.29 public static class PagerHelper { #region 数字分页类 /// <summary> /// /// </summar ...
- BZOJ 1101: [POI2007]Zap
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2262 Solved: 895[Submit][Status] ...