// -----------------------------------------------------------------------
// <copyright file="CustomExceptionAttribute.cs" company="技术支持——谭明超">
// Copyright (c) 2016 QS.Web.Extensions. All rights reserved.
// </copyright>
// <last-editor>谭明超</last-editor>
// <last-date>2016/8/2 20:56:16</last-date>
// ----------------------------------------------------------------------- using System;
using System.Web;
using System.Web.Mvc; namespace QS.Web.Extensions
{
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
Exception exception = filterContext.Exception;
if (filterContext.ExceptionHandled == true)
{
return;
}
HttpException httpException = new HttpException(null, exception);
//filterContext.Exception.Message可获取错误信息 /*
* 1、根据对应的HTTP错误码跳转到错误页面
* 2、这里对HTTP 404/400错误进行捕捉和处理
* 3、其他错误默认为HTTP 500服务器错误
*/
if (httpException != null && (httpException.GetHttpCode() == || httpException.GetHttpCode() == ))
{
filterContext.HttpContext.Response.StatusCode = ;
filterContext.HttpContext.Response.Write("错误的请求路径");
filterContext.HttpContext.Response.WriteFile("~/HttpError/404.html");
}
else
{
filterContext.HttpContext.Response.StatusCode = ;
filterContext.HttpContext.Response.Write("服务器内部错误");
filterContext.HttpContext.Response.WriteFile("~/HttpError/500.html");
}
/*---------------------------------------------------------
* 这里可进行相关自定义业务处理,比如日志记录等
---------------------------------------------------------*/ //设置异常已经处理,否则会被其他异常过滤器覆盖
filterContext.ExceptionHandled = true; //在派生类中重写时,获取或设置一个值,该值指定是否禁用IIS自定义错误。
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
}

该源码源自网上,,,,不记得哪里的了

MVC Filter自定义异常(拦截)的更多相关文章

  1. 1.5(Spring MVC学习笔记) 拦截器(Interceptor)

    一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...

  2. Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用

    一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...

  3. Spring Mvc 的自定义拦截器

     spring mvc的拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户 ...

  4. Spring mvc interceptor配置拦截器,没有登录跳到登录页

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. MVC Filter 实现方式和作用范围控制

    Asp.Net MVC Filter 实现方式和作用范围控制 MVC中的Filte 简单又优雅的实现了AOP ,在日志,权限,缓存和异常处理等方面用的比较多.但本文不是讨论Filter这些功能点,而是 ...

  6. spring mvc中的拦截器小结 .

    在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...

  7. 学习之-ASP.NET MVC Filter

    MVC Filter 是典型的AOP应用,对MVC框架处理客户端请求注入额外的一些逻辑,如日志记录.缓存处理.异常处理和权限验证,性能检测(横切关注点),而这些逻辑通常与主要业务无关,被独立分开作为公 ...

  8. 利用Filter和拦截器,将用户信息动态传入Request方法

    前言: 在开发当中,经常会验证用户登录状态和获取用户信息.如果每次都手动调用用户信息查询接口,会非常的繁琐,而且代码冗余.为了提高开发效率,因此就有了今天这篇文章. 思路: 用户请求我们的方法会携带一 ...

  9. Java Filter(拦截器)

    多个Filter按照在配置文件中配置的filter顺序执行. 在web.xml文件中配置该Filter,使用init-param元素为该Filter配置参数,init-param可接受如下两个子元素: ...

随机推荐

  1. How to detect and avoid memory and resources leaks in .NET applications

    By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...

  2. @DataProvider Method 参数传递

    package roger.testng; import java.lang.reflect.Method; import org.testng.annotations.DataProvider; i ...

  3. strtok

    1. Int main(void) { char *tmp = NULL; Char *remotebuf=”0\r\n”; tmp = strtok(remotebuf, DELIM);      ...

  4. Linux查看系统性能命令

    性能调优的第一步是性能分析,下面从性能分析着手进行一些介绍,尤其对linux性能分析工具vmstat的用法和实践进行详细介绍. ———————————————————————————————————— ...

  5. Cocos2d-x v3.3 lua绑定c++类方法总结

    网上有很多cocos2d-x lua绑定c++类的接口教程,这篇文章也是总结他们的经验. 其中重点参考了 http://cn.cocos2d-x.org/tutorial/show?id=1295, ...

  6. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  7. 多控制器之UIWindow

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 利用fgets,fputs的回显程序

    #include <stdio.h> #define MAXLINE 20 int main(void) { char line[MAXLINE]; while(fgets(line,MA ...

  9. Html笔记(一)概述

    Html就是超文本标记语言的简写,是最基础的网页语言 Html是通过标签来定义的语言,代码都是由标签所组成 Html代码不用区分大小写 Html代码由<html>开始</html&g ...

  10. weka 集成学习

    import java.io.*;import weka.classifiers.*;import weka.classifiers.meta.Vote;import weka.core.Instan ...