小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

MVC Model Binder

 昵稱10504424 2013-11-13

這篇博客是借助一個(gè)自己寫的工程來理解model binder的過程.


MVC通過路由系統(tǒng),根據(jù)url找到對(duì)應(yīng)的Action,然后再執(zhí)行action,在執(zhí)行action的時(shí)候,根據(jù)action的參數(shù)和數(shù)據(jù)來源比對(duì),生成各個(gè)參數(shù)的值,這就是model binder.

IActionInvoker


MVC中這個(gè)核心處理邏輯都在ControllerActionInvoker里,用reflector看,能看能到這個(gè)類繼承了IActionInvoker接口

所以咱們可以根據(jù)代碼模擬寫出自己的CustomActionInvoker

以下是我自己寫的ActionInvoker類

復(fù)制代碼
 1     public class CustomActionInvoker : IActionInvoker
 2     {
 3 
 4         public bool InvokeAction(ControllerContext controllerContext, string actionName)
 5         {
 6             bool flag = false;
 7             try
 8             {
 9                 //get controller type
10                 Type controllerType = controllerContext.Controller.GetType();
11                 //get controller descriptor
12                 ControllerDescriptor controllerDescriptor = 
new ReflectedControllerDescriptor(controllerType); 13 //get action descriptor 14 ActionDescriptor actionDescriptor =
controllerDescriptor.FindAction(controllerContext, actionName);
15 Dictionary<string, object> parameters =

new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); 16 //get parameter-value entity 17 foreach (ParameterDescriptor parameterDescriptor in actionDescriptor.GetParameters()) 18 { 19 Type parameterType = parameterDescriptor.ParameterType; 20 //get model binder 21 IModelBinder modelBinder = new CustomModelBinder(); 22 IValueProvider valueProvider = controllerContext.Controller.ValueProvider; 23 string str = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName; 24 ModelBindingContext bindingContext = new ModelBindingContext(); 25 bindingContext.FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null; 26 bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType); 27 bindingContext.ModelName = str; 28 bindingContext.ModelState = controllerContext.Controller.ViewData.ModelState; 29 bindingContext.ValueProvider = valueProvider; 30 parameters.Add(parameterDescriptor.ParameterName,
modelBinder.BindModel(controllerContext, bindingContext));
31 } 32 ActionResult result = (ActionResult)actionDescriptor.Execute(controllerContext, parameters); 33 result.ExecuteResult(controllerContext); 34 flag = true; 35 } 36 catch (Exception ex) 37 { 38 //log 39 } 40 return flag; 41 } 42 }
復(fù)制代碼

以下詳細(xì)解釋下執(zhí)行過程

*Descriptor


執(zhí)行過程中涉及到三個(gè)Descriptor,ControllerDescriptor,ActionDescriptor,ParameterDescriptor

ControllerDescriptor主要作用是根據(jù)action name獲取到ActionDescriptor,代碼中使用的是MVC自帶的ReflectedControllerDescriptor,從名字就可以看出來,主要是靠反射獲取到action.

ActionDescriptor,主要作用是獲取parameterDescriptor,然后execute action.

parameterDescriptor,描述的是action的參數(shù)信息,包括name、type等

ModelBinder


最核心的方法. 將傳遞的數(shù)據(jù)和參數(shù)一一對(duì)應(yīng),筆者是自己寫的CustomModelBinder,MVC默認(rèn)用的是DefaultModelBinder 都實(shí)現(xiàn)了接口IModelBinder

其中CustomModelBinder的代碼如下

注:我只是實(shí)現(xiàn)了簡(jiǎn)單的基本類型

中間有最核心的方法

ValueProvider


MVC默認(rèn)提供了幾種ValueProvider,每種都有對(duì)應(yīng)的ValueProviderFactory,每種ValueProvider都對(duì)應(yīng)著自己的數(shù)據(jù)源

注冊(cè)ActionInvoker


上述過程講完之后,還缺一個(gè)怎么應(yīng)用上自己寫的ActionInvoker,在Controller里提供了虛方法CreateActionInvoker

到此,整個(gè)過程已講完。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多