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 handleerrorattrite) 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:
02 |
Using System. Collections. Generic; |
05 |
Using System. Web. MVC; |
07 |
Namespace Tew.vcwebapp1.actionresultex |
09 |
Public Class Showmessageattribute: actionfilterattribute |
11 |
Public String Message {Get;Set;} |
12 |
Public Override Void Onactionexecuting (actionexecutingcontext filtercontext) |
14 |
Filtercontext. httpcontext. response. Write ("[Beforeaction" + Message +"]"); |
16 |
Public Override Void Onactionexecuted (actionexecutedcontext filtercontext) |
18 |
Filtercontext. httpcontext. response. Write ("[Afteraction" + Message +"]"); |
20 |
Public Override Void Onresultexecuting (resultexecutingcontext filtercontext) |
22 |
Filtercontext. httpcontext. response. Write ("[Beforeresult" + Message +"]"); |
24 |
Public Override Void Onresultexecuted (resultexecutedcontext filtercontext) |
26 |
Filtercontext. httpcontext. response. Write ("[Afterresult" + Message +"]"); |
Usage:
02 |
Using System. Collections. Generic; |
05 |
Using System. Web. MVC; |
06 |
Using Tew.vcwebapp1.actionresultex; |
07 |
Using System. Data. sqlclient; |
09 |
Namespace Tew.vcwebapp1.controllers |
11 |
Public Class Showmessagedemocontroller: Controller |
14 |
// Get:/showmessagedemo/ |
15 |
[Showmessage (Message ="This is my information" , Order = 1)] |
16 |
[Handleerror (view ="Problem")] |
17 |
Public Actionresult index () |
20 |
Sqlconnection conn =New Sqlconnection (); |
23 |
Return Content ("Webpage content"); |
Output result:
[Beforeaction: This is my information. A] [afteraction: This is my information. A]
[Beforeresult this is my information A] webpage content [afterresult this is my information 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 handleerrorattrite) 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:
02 |
Using System. Collections. Generic; |
05 |
Using System. Web. MVC; |
07 |
Namespace Tew.vcwebapp1.actionresultex |
09 |
Public Class Showmessageattribute: actionfilterattribute |
11 |
Public String Message {Get;Set;} |
12 |
Public Override Void Onactionexecuting (actionexecutingcontext filtercontext) |
14 |
Filtercontext. httpcontext. response. Write ("[Beforeaction" + Message +"]"); |
16 |
Public Override Void Onactionexecuted (actionexecutedcontext filtercontext) |
18 |
Filtercontext. httpcontext. response. Write ("[Afteraction" + Message +"]"); |
20 |
Public Override Void Onresultexecuting (resultexecutingcontext filtercontext) |
22 |
Filtercontext. httpcontext. response. Write ("[Beforeresult" + Message +"]"); |
24 |
Public Override Void Onresultexecuted (resultexecutedcontext filtercontext) |
26 |
Filtercontext. httpcontext. response. Write ("[Afterresult" + Message +"]"); |
Usage:
02 |
Using System. Collections. Generic; |
05 |
Using System. Web. MVC; |
06 |
Using Tew.vcwebapp1.actionresultex; |
07 |
Using System. Data. sqlclient; |
09 |
Namespace Tew.vcwebapp1.controllers |
11 |
Public Class Showmessagedemocontroller: Controller |
14 |
// Get:/showmessagedemo/ |
15 |
[Showmessage (Message ="This is my information" , Order = 1)] |
16 |
[Handleerror (view ="Problem")] |
17 |
Public Actionresult index () |
20 |
Sqlconnection conn =New Sqlconnection (); |
23 |
Return Content ("Webpage content"); |
Output result:
[Beforeaction: This is my information. A] [afteraction: This is my information. A]
[Beforeresult this is my information A] webpage content [afterresult this is my information A]
<Customerrors mode = "on"> </customerrors>