控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVC过滤器.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index(string id, string name)
{
int a = 1;
int b = 0;
int c = a / b; //这里人为的搞一个错误。
return View();
} public ActionResult Error()
{
return View();
} }
}

Home控制器下的的Error视图

@{
Layout = null;
}
@model HandleErrorInfo
@*这个HandleErrorInfo实体类里面就是当前最后一次错误的具体信息*@
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Error</title>
</head>
<body>
<div>
@Model.ActionName;
@Model.ControllerName;
@Model.Exception.Message;
</div>
</body>
</html>

在项目下建一个Filters的目录,用来放过滤器。

在Filters目录以下新建一个ExceptionAttribute.cs异常过滤器类。让它继承HandleErrorAttribute类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVC过滤器.Filters
{
public class ExceptionAttribute:HandleErrorAttribute
{ public override void OnException(ExceptionContext filterContext)
{
//获取抛出异常的对象
Exception ex = filterContext.Exception; //写日记
System.IO.File.AppendAllText(filterContext.HttpContext.Server.MapPath("/Logs/Log.txt"), ex.ToString()); //假设这里设为false。就表示告诉MVC框架,我没有处理这个错误。 然后让它跳转到自定义的错误页(设为true的话。就表示告诉MVC框架。异常我已经处理了。不须要在跳转到错误页了,也部会抛出黄页了)
filterContext.ExceptionHandled = false; }
}
}

去到APP_Start目录下的FilterConfig.cs类中,去将自定义的ExceptionAttribute异常过滤器注冊为全局过滤器

using MVC过滤器.Filters;
using System.Web;
using System.Web.Mvc; namespace MVC过滤器
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{ filters.Add(new HandleErrorAttribute()); //将自定义的异常过滤器注冊为全局过滤器。(全局过滤器是能够注冊多个的)
filters.Add(new ExceptionAttribute());
}
}
}

MVC异常过滤器 (错误页)的更多相关文章

  1. 如何访问mvc 默认的错误页

    在ActionResult 中: public ActionResult Error() { return View("~/Views/Shared/Error.cshtml"); ...

  2. ASP.NET Core 中间件 自定义全局异常中间件以及 MVC异常过滤器作用

    中间件是一种装配到应用管道以处理请求和响应的软件. 每个组件: 选择是否将请求传递到管道中的下一个组件. 可在管道中的下一个组件前后执行工作. 请求委托用于生成请求管道. 请求委托处理每个 HTTP ...

  3. ASP.NET MVC下自定义错误页和展示错误页的几种方式

    在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...

  4. MVC异常过滤器

    MVC过滤器 一般的过滤器执行顺序 IAuthorizationFilter->OnAuthorization(授权) IActionFilter          ->OnActionE ...

  5. MVC异常过滤器在三种作用范围下的执行顺序

    对于一般过滤器(即:除了IExceptionFilter ),当同时在Controller和Action中都设置了同一个过滤器后(例如IActionFilter),执行顺序一般是由外到里,即“全局”- ...

  6. MVC与WebApi中的异常过滤器

    一.MVC的异常过滤器   1.自定义MVC异常过滤器 创建一个类,继承HandleErrorAttribute即可,如果不需要作为特性使用直接实现IExceptionFilter接口即可, 注意,该 ...

  7. ASP.NET MVC-异常处理&自定义错误页

    一.应用场景 对于B/S应用程序,在部署到正式环境运行的过程中,很有可能出现一些在前期测试过程中没有发现的一些异常或者错误,或者说只有在特定条件满足时才会发生的一些异常,对于使用ASP.NET MVC ...

  8. 笨鸟先飞之ASP.NET MVC系列之过滤器(06异常过滤器)

    概念介绍 异常过滤器主要在我们方法中出现异常的时候触发,一般我们用 异常过滤器 记录日志,或者在产生异常时做友好的处理 如果我们需要创建异常过滤器需要实现IExceptionFilter接口. nam ...

  9. [Asp.net MVC]HandleErrorAttribute异常过滤器

    摘要 在asp.net mvc中除了使用try...catch/finally来处理异常外,它提供了一种通过在Controller或者Action上添加特性的方式来处理异常. HandleErrorA ...

随机推荐

  1. java设计模式之策略模式总结

    策略模式的定义:(定义截自http://www.cnblogs.com/whgk/p/6087064.html) 1.策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立 ...

  2. Clickhouse DDL&DML

    (1)添加列: alter table [db.]table_name add column column_name [type] [default_expr] [after name_after] ...

  3. Crash (computing)

    In computing, a crash (or system crash) occurs when a computer program, such as a software applicati ...

  4. SQL关于触发器及存储过程的创建

    使用T-SQL语句来创建触发器   基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Ins ...

  5. python多进程和多线程编程

    17 多线程和多进程并发 The modules described in this chapter provide support for concurrent execution of code. ...

  6. [Algorithm] 5. Kth Largest Element

    Description Find K-th largest element in an array. Example In array [9,3,2,4,8], the 3rd largest ele ...

  7. Oracle 常用目录结构(10g)

    大多数情况下,了解Oracle常用目录结构,将大大提高数据库管理与维护的工作效率,本文介绍了Oracle 10g 的常用目录. OFA: 下面给出Oracle 最优灵活体系结构OFA(Optimal ...

  8. mac 中查看监听程序

    sudo lsof -nP -iTCP -sTCP:LISTEN | grep mysql

  9. python之cookbook-day01

    第一章:数据结构和算法 1.1 解压序列赋值给多个变量 >>> p = (4, 5) >>> x, y = p >>> x 4 >>& ...

  10. scanf与getchar

    如下: 5  5 R  R  R  R  R R  R  R  R  R R  R  R  R  R R  R  R  R  R R  R  R  R  R 只允许用scanf,如何写读取函数. 由于 ...