Next: The activation process of the MVC controller
First, the code is in force, the pseudo-coderoughlyResolves the process of action execution
Try{Run each iauthorizationfilter's Onauthorization () method if(none of the iauthorizationfilters cancelled execution) {Run each iactionfilter's OnActionExecuting () methodrun the action method run each Iactionfilter's OnActionExecuted () method (in reverse order)Run each Iresultfilter's Onresultexecuting () methodrun the action result Run each iresultfilter's Onresultexecuted () method (in reverse order) } Else{Run Any action resultSetBy the Authorization filters}} Catch(Exception not handled by any action or result filter) {Run each iexceptionfilter's Onexception () methodRun Any action resultSetBy the exception filters}
Second, return to the main battlefield action execution method
Public Virtual BOOLInvokeaction (ControllerContext ControllerContext,stringactionname) { if(ControllerContext = =NULL) { Throw NewArgumentNullException ("ControllerContext"); } if(string. IsNullOrEmpty (ActionName)) {Throw NewArgumentException (Mvcresources.common_nullorempty,"ActionName"); }
//Description of information about the controller Controllerdescriptor Controllerdescriptor= This. Getcontrollerdescriptor (ControllerContext);
//Description of relevant action related information Actiondescriptor Actiondescriptor= This. Findaction (ControllerContext, Controllerdescriptor, ActionName); if(Actiondescriptor! =NULL) {
//Gets all the filter for the action FilterInfo Filters= This. Getfilters (ControllerContext, actiondescriptor); Try{authorizationcontext AuthorizationContext= This. Invokeauthorizationfilters (ControllerContext, filters. Authorizationfilters, Actiondescriptor); if(Authorizationcontext.result! =NULL) { This. Invokeactionresult (ControllerContext, Authorizationcontext.result); } Else { if(controllerContext.Controller.ValidateRequest) {
//The Earth man should know what this thing does, you know? controlleractioninvoker.validaterequest (ControllerContext); } IDictionary<string,Object> parametervalues = This. Getparametervalues (ControllerContext, actiondescriptor); ActionExecutedContext ActionExecutedContext= This. Invokeactionmethodwithfilters (ControllerContext, filters. Actionfilters, Actiondescriptor, parametervalues); This. Invokeactionresultwithfilters (ControllerContext, filters. Resultfilters, Actionexecutedcontext.result); } } Catch(ThreadAbortException) {Throw; } Catch(Exception Exception) {exceptioncontext Exceptioncontext= This. Invokeexceptionfilters (ControllerContext, filters. Exceptionfilters, exception); if(!exceptioncontext.exceptionhandled) {Throw; } This. Invokeactionresult (ControllerContext, Exceptioncontext.result); } return true; } return false; }
The authorization process above
This . Invokeauthorizationfilters (ControllerContext, filters. Authorizationfilters, actiondescriptor); if NULL ) { this. Invokeactionresult (ControllerContext, Authorizationcontext.result); }
protected VirtualAuthorizationContext invokeauthorizationfilters (ControllerContext controllercontext, IList< Iauthorizationfilter>filters, Actiondescriptor actiondescriptor) {AuthorizationContext AuthorizationContext=NewAuthorizationContext (ControllerContext, actiondescriptor); foreach(Iauthorizationfilter currentinchfilters) {current . Onauthorization (AuthorizationContext);//Obviously when we customize Iauthorizationfilter, we do what we define here. If
In this process,the Result of AuthorizationContext is assigned to you, then all other authorization certificates will be terminated and the system will fully execute this . Invokeactionresult (ControllerContext, Authorizationcontext.result);
if (authorizationcontext.result! = null) { break; } } returnAuthorizationContext;}
Third, action together with the implementation of the filter, the above talk about the implementation of the authorization filter
idictionary<stringobjectthis. Getparametervaluesthis. Invokeactionmethodwithfilters (ControllerContext, filters. Actionfilters, Actiondescriptor, parametervalues); this. Invokeactionresultwithfilters (ControllerContext, filters. Resultfilters, Actionexecutedcontext.result);
protected Virtualidictionary<string,Object>getparametervalues (ControllerContext controllercontext, Actiondescriptor actiondescriptor) {Dictionary<string,Object> dictionary =Newdictionary<string,Object>(stringcomparer.ordinalignorecase); parameterdescriptor[] Parameters=actiondescriptor.getparameters (); Parameterdescriptor[] Array=parameters; for(inti =0; I < array. Length; i++) {Parameterdescriptor parameterdescriptor=Array[i]; Dictionary[parameterdescriptor.parametername]= This. Getparametervalue (ControllerContext, Parameterdescriptor); } returndictionary;}
protected Virtual ObjectGetparametervalue (ControllerContext controllercontext, Parameterdescriptor parameterdescriptor) {Type ParameterType=Parameterdescriptor.parametertype; Imodelbinder Modelbinder = This. Getmodelbinder (parameterdescriptor); Ivalueprovider Valueprovider=ControllerContext.Controller.ValueProvider; stringModelName = ParameterDescriptor.BindingInfo.Prefix??Parameterdescriptor.parametername; predicate<string> propertyfilter =Controlleractioninvoker.getpropertyfilter (Parameterdescriptor); Modelbindingcontext BindingContext=NewModelbindingcontext {fallbacktoemptyprefix= ParameterDescriptor.BindingInfo.Prefix = =NULL, Modelmetadata= ModelMetadataProviders.Current.GetMetadataForType (NULL, ParameterType), ModelName=modelname, Modelstate=controllerContext.Controller.ViewData.ModelState, PropertyFilter=PropertyFilter, Valueprovider=Valueprovider}; Objectobj =Modelbinder. Bindmodel (ControllerContext, BindingContext); returnObj??Parameterdescriptor.defaultvalue;}
Private imodelbinder Getmodelbinder (parameterdescriptor parameterdescriptor) {
Is the//action parameter included with the binding information? If you do not use the system default way to bind the returnthis. Binders.getbinder (Parameterdescriptor.parametertype);}
Privateimodelbinder Getbinder (Type modeltype, Imodelbinder fallbackbinder) {imodelbinder Modelbinder= This. _modelbinderproviders.getbinder (Modeltype); if(Modelbinder! =NULL) { returnModelbinder; } if( This. _innerdictionary.trygetvalue (Modeltype, outModelbinder)) { returnModelbinder; } Modelbinder= Modelbinders.getbinderfromattributes (Modeltype, () = =string. Format (CultureInfo.CurrentCulture, Mvcresources.modelbinderdictionary_multipleattributes,New Object[] {modeltype.fullname}); returnModelbinder??Fallbackbinder;}
Private Staticmodelbinderdictionary createdefaultbinderdictionary () {return Newmodelbinderdictionary {{typeof(httppostedfilebase),NewHttppostedfilebasemodelbinder ()}, {typeof(byte[]), NewBytearraymodelbinder ()}, {typeof(Binary),NewLinqbinarymodelbinder ()}, {typeof(CancellationToken),NewCancellationtokenmodelbinder ()}};}
Next: How the arguments in the action are assigned