现在的位置: 首页 > 移动开发 > 正文

ASP.NET MVCController怎么激活

2020年06月02日 移动开发 ⁄ 共 3018字 ⁄ 字号 评论关闭

  因为MvcHandler实现了IHttpAsyncHandler接口,因此紧接着就会调用BeginProcessRequest方法,这个方法首先会进行一些TrustLevel之类的安全检测。下面学步园小编来讲解下ASP.NETMVCController怎么激活?

  ASP.NETMVCController怎么激活

  privatevoidProcessRequestInit(HttpContextBasehttpContext,outIControllercontroller,outIControllerFactoryfactory){

  //Getthecontrollertype

  stringcontrollerName=RequestContext.RouteData.GetRequiredString("controller");

  //InstantiatethecontrollerandcallExecute

  factory=ControllerBuilder.GetControllerFactory();

  controller=factory.CreateController(RequestContext,controllerName);

  if(controller==null){

  thrownewInvalidOperationException(

  String.Format(

  CultureInfo.CurrentCulture,

  MvcResources.ControllerBuilder_FactoryReturnedNull,

  factory.GetType(),

  controllerName));

  }

  }

  首先会获得controller的名字,然后会实例化controller,这里采用了抽象工厂的模式,首先利用ControllerBuilder获得一个IControllerFactory的实例,ControllerBuilder采用DependencyInjection来实例化IControllerFactory,关于MVC中DI的实现以后另文介绍,在默认情况下,ControllerBuilder会返回一个实例。接着调用CreateController方法:

  publicvirtualIControllerCreateController(RequestContextrequestContext,stringcontrollerName){

  TypecontrollerType=GetControllerType(requestContext,controllerName);

  IControllercontroller=DefaultControllerFactoryGetControllerInstance(requestContext,controllerType);

  returncontroller;

  }

  方法分为两步,先获得类型,再获得实例:

  ASP.NETMVCController怎么激活

  protectedinternalvirtualTypeGetControllerType(RequestContextrequestContext,stringcontrollerName){

  //firstsearchinthecurrentroute'snamespacecollection

  objectrouteNamespacesObj;

  Typematch;

  if(requestContext!=null&&requestContext.RouteData.DataTokens.TryGetValue("Namespaces",outrouteNamespacesObj)){

  IEnumerablerouteNamespaces=routeNamespacesObjasIEnumerable;

  if(routeNamespaces!=null&&routeNamespaces.Any()){

  HashSetnsHash=newHashSet(routeNamespaces,StringComparer.OrdinalIgnoreCase);

  match=GetControllerTypeWithinNamespaces(requestContext.RouteData.Route,controllerName,nsHash);

  //theUseNamespaceFallbackkeymightnotexist,inwhichcaseitsvalueisimplicitly"true"

  if(match!=null||false.Equals(requestContext.RouteData.DataTokens["UseNamespaceFallback"])){

  //gotamatchortherouterequestedwestoplooking

  returnmatch;

  }

  }

  }

  //thensearchintheapplication'sdefaultnamespacecollection

  if(ControllerBuilder.DefaultNamespaces.Count>0){

  HashSetnsDefaults=newHashSet(ControllerBuilder.DefaultNamespaces,StringComparer.OrdinalIgnoreCase);

  match=GetControllerTypeWithinNamespaces(requestContext.RouteData.Route,controllerName,nsDefaults);

  if(match!=null){

  returnmatch;

  }

  }

  //ifallelsefails,searcheverynamespace

  returnGetControllerTypeWithinNamespaces(requestContext.RouteData.Route,controllerName,null/*namespaces*/);

  }

  DefaultControllerFactory在根据路由信息查找对应Controller的类型的时候,首先判断DataToken中有没有Namespace,然后调用GetControllerTypeWithinNamespaces方法查找Controller对应的类。

  但是ASP.NETMVC在这里使用了较为复杂的DI机制,在默认情况下,它调用的是DefaultDependencyResolver的GetService方法,这个方法最终也仅仅是调用了Activator.CreateInstance方法。关于MVC中的DI机制,这里不多做分析,另文叙述。至此,一个Controller类已经被构造出来了。

  以上就是关于“ASP.NETMVCController怎么激活”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

  

抱歉!评论已关闭.