5. sessionstateattribute
When constructing the sessionstateattribute object, you must pass in a sessionstatebehavior enumeration value, which can be obtained through the behavior attribute of attribute.
This attribute can only be used for reference types (attributetargets. Class). Repeated use is not allowed (allowmultiple = false) and inheritance is allowed (inherited = true ).
6. mvcroutehandler
The mvcroutehandler class adds an object that accepts the icontrollerfactory interface implementation class as the constructor for parameter passing. The constructor allows the input of null values. The constructor simply assigns the parameter to a private Variable _ controllerfactory.
A virtual getsessionstatebehavior method is added to accept the requestcontext parameter and return a sessionstatebehavior object.
Create an icontrollerfactory local variable named controllerfactory in the method.
If the private Variable _ controllerfactory is not null, it is assigned to controllerfactory. Otherwise, the default controllerfactory is obtained through controllerbuilder. Current. getcontrollerfactory (). (Note that it is not necessarily defacontrocontrollerfactory. Then, return the execution result of the getcontrollersessionbehavior method of controllerfactory.
The getcontrollersessionbehavior method is added to the icontrollerfactory interface.
In defaultcontrollerfactory, getcontrollersessionbehavior obtains the sessionstateattribute of the corresponding controller through reflection (which will be cached) and returns its behavior attribute.
In the gethttphandler method of the mvcroutehandler class, the getsessionstatebehavior method is called to obtain a sessionstatebehavior enumeration value, and the sessionstate of the current request is set through setsessionstatebehavior of the current request context. Of course, the gethttphandler method returns a new mvchandler object.
It is not difficult to see a new extension point provided by mvc3:
1. Create mvcroutehandler (routecollectionextensions. maproute is an object created through the no-argument constructor method) by accepting the icontrollerfactory interface to implement the class object as the constructor for parameter passing ).
2. Implement the New icontrollerfactory to obtain the sessionstatebehavior corresponding to the Controller in a non-default way (for example, through direct cache instead of reflection and then cache), or set global sessionstatebehavior in some namespaces.
3. You can even implement actionsessionstateattribute to set the sessionstatebehavior of an action instead of the entire controller.
VII. icontrolleractivator and defaultcontrollerfactory
As we know, controllerfactory is generated to create a controller, which is also mentioned in the above content.
In the defaultcontrollerfactory class in mvc3, make the method of creating the Controller more flexible and work with DI (see denpendencyresolver for details ).
First, a public constructor with icontrolleractivator interface parameters is added.
The unique method of the icontrolleractivator interface is similar to the getcontrollerinstance method.
We know that the getcontrollerinstance method in the defaultcontrollerfactory class of MVC2 directly uses the activator. createinstance class to create an object.
Through the common constructors with parameters added in mvc3, we can easily change this method. In mv2, if we want to change, we need to implement an icontrollerfactory or inherit the defaultcontrollerfactory class to override the getcontrollerinstance method.
Defaultcontrollerfactory also provides an internal constructor with three parameters. The implementation mechanism of this method is similar to controllerbuilder.
As you can imagine, if the constructor is public, we can use defaultcontrollerfactory to destroy many previously MVC2 controllerfactory implemented only to create a controller class with parameters.
8. Filter
Mvc3 adds filter-related classes or enumerations:
Filter, filterprovidercollection, filterproviders,
Globalfiltercollection, globalfilters,
Filterattributefilterprovider, controllerinstancefilterprovider, and filterscope
filter has been discussed for a long time before:
Asp. net MVC: Do not want to write the same fitlerattribute on multiple actions
Asp. net MVC: Do not want to write the same fitlerattribute (lower) on multiple actions