asp.net mvc filter

來源:互聯網
上載者:User

Using Filters to Attach Reusable Behaviors

 

Introducing the Four Basic Types of Filters

 

 

Notice that ActionFilterAttribute is the default implementation for both IActionFilter
and IResultFilter—it implements both of those interfaces. It’s meant to be totally general
purpose, so it doesn’t provide any implementation (in fact, it’s marked abstract, so you can
only use it by deriving a subclass from it). However, the other default implementations
(AuthorizeAttribute and HandleErrorAttribute) are concrete, contain useful logic, and can
be used without deriving a subclass.
To get a better understanding of these types and their relationships, examine Figure 9-6.
It shows that all filter attributes are derived from FilterAttribute, and also implement one or
more of the filter interfaces. The dark boxes represent ready-to-use concrete filters; the rest are
interfaces or abstract base classes. Later in this chapter, you’ll learn more about each built-in
filter type.

 

MyFilter Demo:

 

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Web;
05 using System.Web.Mvc;
06  
07 namespace TestMvcWebApp1.ActionResultEx
08 {
09     public class ShowMessageAttribute:ActionFilterAttribute
10     {
11         public string Message { get; set; }
12         public override void OnActionExecuting(ActionExecutingContext filterContext)
13         {
14             filterContext.HttpContext.Response.Write("[BeforeAction " + Message + "]");
15         }
16         public override void OnActionExecuted(ActionExecutedContext filterContext)
17         {
18             filterContext.HttpContext.Response.Write("[AfterAction " + Message + "]");
19         }
20         public override void OnResultExecuting(ResultExecutingContext filterContext)
21         {
22             filterContext.HttpContext.Response.Write("[BeforeResult " + Message + "]");
23         }
24         public override void OnResultExecuted(ResultExecutedContext filterContext)
25         {
26             filterContext.HttpContext.Response.Write("[AfterResult " + Message + "]");
27         }
28     }
29 }

 

 

 

使用:

 

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Web;
05 using System.Web.Mvc;
06 using TestMvcWebApp1.ActionResultEx;
07 using System.Data.SqlClient;
08  
09 namespace TestMvcWebApp1.Controllers
10 {
11     public class ShowMessageDemoController : Controller
12     {
13         //
14         // GET: /ShowMessageDemo/
15         [ShowMessage(Message = "這是我的資訊A" , Order=1)]
16         [HandleError(View="Problem")]
17         public ActionResult Index()
18         {
19             int a = 0;
20             SqlConnection conn = new SqlConnection();
21             conn.Open();
22  
23             return Content("  網頁內容  ");
24         }
25  
26     }
27 }

 

輸出結果:

[BeforeAction 這是我的資訊A][AfterAction 這是我的資訊A]

[BeforeResult 這是我的資訊A]  網頁內容  [AfterResult 這是我的資訊A]

 

 <customErrors mode="On"></customErrors>

 

Using Filters to Attach Reusable Behaviors

 

Introducing the Four Basic Types of Filters

 

 

Notice that ActionFilterAttribute is the default implementation for both IActionFilter
and IResultFilter—it implements both of those interfaces. It’s meant to be totally general
purpose, so it doesn’t provide any implementation (in fact, it’s marked abstract, so you can
only use it by deriving a subclass from it). However, the other default implementations
(AuthorizeAttribute and HandleErrorAttribute) are concrete, contain useful logic, and can
be used without deriving a subclass.
To get a better understanding of these types and their relationships, examine Figure 9-6.
It shows that all filter attributes are derived from FilterAttribute, and also implement one or
more of the filter interfaces. The dark boxes represent ready-to-use concrete filters; the rest are
interfaces or abstract base classes. Later in this chapter, you’ll learn more about each built-in
filter type.

 

MyFilter Demo:

 

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Web;
05 using System.Web.Mvc;
06  
07 namespace TestMvcWebApp1.ActionResultEx
08 {
09     public class ShowMessageAttribute:ActionFilterAttribute
10     {
11         public string Message { get; set; }
12         public override void OnActionExecuting(ActionExecutingContext filterContext)
13         {
14             filterContext.HttpContext.Response.Write("[BeforeAction " + Message + "]");
15         }
16         public override void OnActionExecuted(ActionExecutedContext filterContext)
17         {
18             filterContext.HttpContext.Response.Write("[AfterAction " + Message + "]");
19         }
20         public override void OnResultExecuting(ResultExecutingContext filterContext)
21         {
22             filterContext.HttpContext.Response.Write("[BeforeResult " + Message + "]");
23         }
24         public override void OnResultExecuted(ResultExecutedContext filterContext)
25         {
26             filterContext.HttpContext.Response.Write("[AfterResult " + Message + "]");
27         }
28     }
29 }

 

 

 

使用:

 

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Web;
05 using System.Web.Mvc;
06 using TestMvcWebApp1.ActionResultEx;
07 using System.Data.SqlClient;
08  
09 namespace TestMvcWebApp1.Controllers
10 {
11     public class ShowMessageDemoController : Controller
12     {
13         //
14         // GET: /ShowMessageDemo/
15         [ShowMessage(Message = "這是我的資訊A" , Order=1)]
16         [HandleError(View="Problem")]
17         public ActionResult Index()
18         {
19             int a = 0;
20             SqlConnection conn = new SqlConnection();
21             conn.Open();
22  
23             return Content("  網頁內容  ");
24         }
25  
26     }
27 }

 

輸出結果:

[BeforeAction 這是我的資訊A][AfterAction 這是我的資訊A]

[BeforeResult 這是我的資訊A]  網頁內容  [AfterResult 這是我的資訊A]

 

 <customErrors mode="On"></customErrors>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.