implement interceptor
in ASP. net mvc, three interceptors are available: Action interceptor, result interceptor, and exception interceptor. In fact, the so-called ASP. net mvc interceptor is nothing mysterious, just a common class. Only the filterattribute base class must be inherited. The action interceptor must implement the iactionfilter interface, while the exception interceptor must implement the iexceptionfilter interface.
Let's first look at the implementation: Let's create a new filters directory under the controllers directory, and then create two classes under the filters directory, one is accountfilter. CS and the other is predictionfilter. The first is the Code of accountfilter. CS.
accountfilter. CS:
Accountfilter. csusing system. Web. MVC; namespace mvcweb. controllers. Filters {public class accountfilter: actionfilterattribute {// before the action is executed (not logged in successfully. Jump to ~ /Account/loginon ") Public override void onactionexecuting (actionexecutingcontext filtercontext) {var authenticate = structuremap. objectfactory. getinstance <ants. provider. iauthenticateprovider> (); If (! Authenticate. isauthenticated) {filtercontext. Result = new redirectresult ("~ /Account/loginon ");} base. onactionexecuting (filtercontext );}}}
In this case, I have to explain that your interceptor intercepts the action and will inevitably use the action-related content during processing. For example, in our example, you need to add content to the viewdata of the controller where the intercepted action is located. Therefore, the interceptor method has a parameter that indicates that the context of the intercepted action is a logical task.
Next, let's look at the exceptionfilter interceptor, which plays a role when an action exception occurs.
Predictionfilter. CS
Using system. web. MVC; namespace mvcweb. controllers. filters {public class exceptionfilter: filterattribute, iexceptionfilter {void iexceptionfilter. onexception (exceptioncontext filtercontext) {filtercontext. controller. viewdata ["error"] = filtercontext. exception; filtercontext. result = new viewresult () {viewname = "error", viewdata = filtercontext. controller. viewdata,}; filtercontext. exceptionhandled = true ;}}}
The exception interceptor must inherit the filterattribute, but does not implement iactionfilter. Instead, it must implement the iexceptionfilter interface. This interface has only one method: onexception, which is called when an exception occurs. Let's take a look at what I asked it to do: first, the exception information (exceptioncontext is also the context, and its member exception is an exception type instance, that is, the thrown exception) record it to the corresponding key value of viewdata, and then we will display the error view.
Note! This is not a controller, but another class, so you cannot call the view method to return the viewresult instance. We had to create a new viewresult instance and set its view name to error. We passed the dataview in the context.
The last line of filtercontext. effecitonhandled = true; is very important. This line tells the system that the exception has been handled and should not be processed again. When the exceptionfilter action is applied, try… is not required... The exception blocker successfully intercepts the exception and processes it accordingly.
Application interceptor
Okay. After the interceptor is created, how can it be applied to the corresponding action? In ASP. net mvc, the application interceptor is easy and pleasant. You only need to write the interceptor as an attribute on the action to apply the interceptor. For example:
[filters. accountfilter] [filters. predictionfilter] public actionresult test () {}