Asp.net MVC Filter

Source: Internet
Author: 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 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:

 

01 Using System;
02 Using System. Collections. Generic;
03 Using System. LINQ;
04 Using System. Web;
05 Using System. Web. MVC;
06  
07 Namespace Tew.vcwebapp1.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 }

 

 

 

Usage:

 

01 Using System;
02 Using System. Collections. Generic;
03 Using System. LINQ;
04 Using System. Web;
05 Using System. Web. MVC;
06 Using Tew.vcwebapp1.actionresultex;
07 Using System. Data. sqlclient;
08  
09 Namespace Tew.vcwebapp1.controllers
10 {
11 Public Class Showmessagedemocontroller: Controller
12 {
13 //
14 // Get:/showmessagedemo/
15 [Showmessage (Message ="This is my information" , 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 ("Webpage content");
24 }
25  
26 }
27 }

 

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:

 

01 Using System;
02 Using System. Collections. Generic;
03 Using System. LINQ;
04 Using System. Web;
05 Using System. Web. MVC;
06  
07 Namespace Tew.vcwebapp1.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 }

 

 

 

Usage:

 

01 Using System;
02 Using System. Collections. Generic;
03 Using System. LINQ;
04 Using System. Web;
05 Using System. Web. MVC;
06 Using Tew.vcwebapp1.actionresultex;
07 Using System. Data. sqlclient;
08  
09 Namespace Tew.vcwebapp1.controllers
10 {
11 Public Class Showmessagedemocontroller: Controller
12 {
13 //
14 // Get:/showmessagedemo/
15 [Showmessage (Message ="This is my information" , 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 ("Webpage content");
24 }
25  
26 }
27 }

 

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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.