码迷,mamicode.com
首页 > 其他好文 > 详细

过滤器

时间:2015-12-09 00:12:03      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

1.在Models中添加控制器类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 namespace MvcValidateDemo.Models
10 {
11     //AllowMultiple =true允许多个标签同时起作用
12     //[AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
13    public  class MyActionFilterAttribute:ActionFilterAttribute
14     {
15         //Action执行之前执行方法
16        public string Name { get; set; }
17         public override void OnActionExecuting(ActionExecutingContext filterContext)
18         {
19             base.OnActionExecuting(filterContext);
20             //HttpContext.Current.Session[""];
21             HttpContext.Current.Response.Write("<br/>OnActionExecuting"+Name);
22         }
23         //Action执行之后
24         public override void OnActionExecuted(ActionExecutedContext filterContext)
25         {
26             base.OnActionExecuted(filterContext);
27             HttpContext.Current.Response.Write("<br/>OnActionExecuted" + Name);
28         }
29         //ActionResult执行之前执行此方法
30         public override void OnResultExecuting(ResultExecutingContext filterContext)
31         {
32             base.OnResultExecuting(filterContext);
33             HttpContext.Current.Response.Write("<br/>OnReslutExecuting" + Name);
34         }
35         //ActionResult执行之后
36         public override void OnResultExecuted(ResultExecutedContext filterContext)
37         {
38             base.OnResultExecuted(filterContext);
39             HttpContext.Current.Response.Write("<br/>OnReslutExecuted" + Name);
40         }
41     }
42 }

2.添加Home控制器

 1 using MvcValidateDemo.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 
 8 namespace MvcValidateDemo.Controllers
 9 {
10     [MyActionFilter(Name = "Home Action")]
11     public class HomeController : Controller
12     {
13         
14         // GET: Home
15         [MyActionFilter(Name = "Index Action")]
16         public ActionResult Index()
17         {
18             Response.Write("<p>Action执行了</p>");
19             return Content("OK,视图被渲染");
20         }
21         //[MyActionFilter(Name="About  Action")]
22         public ActionResult About()
23         {
24             //throw new Exception("demo");
25             return Content("<p>About 渲染</p>");
26         }
27     }
28 }

 

全局异常过滤器

1.在Models中添加MyExceptionFilterAttribute控制器类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace _111111.Models
 8 {
 9     public class MyEexeptionFilterAttribute : HandleErrorAttribute
10     {
11         public override void OnException(ExceptionContext filterContext)
12         {
13             base.OnException(filterContext);
14 
15             HttpContext.Current.Response.Redirect("/Home/Index");
16         }
17     }
18 }

在Home/About中手动构建一个异常

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace _111111.Controllers
 8 {
 9     public class HomeController : Controller
10     {
11         //
12         // GET: /Home/
13 
14         public ActionResult Index()
15         {
16             return Content("dsjfjsj");
17         }
18         public ActionResult About()
19         {
20             throw new Exception("demo");
21         }
22 
23     }
24 }

将自己的异常过滤器注册到全局过滤器中

 1 using _111111.Models;
 2 using System.Web;
 3 using System.Web.Mvc;
 4 
 5 namespace _111111
 6 {
 7     public class FilterConfig
 8     {
 9         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
10         {
11             filters.Add(new MyEexeptionFilterAttribute());
12         }
13     }
14 }

 

过滤器

标签:

原文地址:http://www.cnblogs.com/mylovemygirl/p/5031359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!