/// <inheritdoc /> public virtual object CreateController([NotNull] ActionContext actionContext) { var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor; if (actionDescriptor == null) { throw new ArgumentException( Resources.FormatActionDescriptorMustBeBasedOnControllerAction( typeof(ControllerActionDescriptor)), nameof(actionContext)); } var controllerType = actionDescriptor.ControllerTypeInfo.AsType(); var controllerTypeInfo = controllerType.GetTypeInfo(); if (controllerTypeInfo.IsValueType || controllerTypeInfo.IsInterface || controllerTypeInfo.IsAbstract || (controllerTypeInfo.IsGenericType && controllerTypeInfo.IsGenericTypeDefinition)) { var message = Resources.FormatValueInterfaceAbstractOrOpenGenericTypesCannotBeActivated( controllerType.FullName, GetType().FullName); throw new InvalidOperationException(message); } var controller = _controllerActivator.Create(actionContext, controllerType); foreach (var propertyActivator in _propertyActivators) { propertyActivator.Activate(actionContext, controller); } return(controller); }
public object CreateController(ControllerContext context) { var controller = controllerActivator.Create(context); attributeInjector.InjectIntoMembers(controller, context.HttpContext.RequestServices); return(controller); }
public IController CreateController(RequestContext requestContext, string controllerName) { Type controllerType = null; string key = requestContext.RouteData.DataTokens["area"] as string; key = string.IsNullOrEmpty(key) ? controllerName : key + controllerName; if (ControllerTypeCache.ContainsKey(key)) { controllerType = ControllerTypeCache[key]; } else { controllerType = GetControllerType(requestContext.RouteData, controllerName); if (controllerType != null) { lock (this) { if (!ControllerTypeCache.ContainsKey(key)) { ControllerTypeCache.Add(key, controllerType); } } } else { throw new HttpException(404, "controller not found"); } } return(_activator.Create(requestContext, controllerType)); }
public void ControllerActivator_Create_ShouldResolveDummyController() { //Arrange var context = MvcMockHelpers.MockHttpContext("~/dummy"); //Act var response = _unityControllerActivator.Create(context.Request.RequestContext, typeof(DummyController)); //Assert Assert.IsNotNull(response); }
public MvcAction BuildAction(RequestContext requestContext) { var actionContext = _actionResolver.Resolve(requestContext); var controllerInstance = (Controller)_controllerActivator.Create(actionContext); var controllerContext = new ControllerContext(controllerInstance); controllerInstance.Request = requestContext; return(new MvcAction(actionContext, controllerContext)); }
public static Controller Create(Type controllerType, IControllerActivator activator, IServiceProvider serviceProvider) { return(activator.Create(new ControllerContext(new ActionContext( new DefaultHttpContext { RequestServices = serviceProvider }, new RouteData(), new ControllerActionDescriptor { ControllerTypeInfo = controllerType.GetTypeInfo() }) )) as Controller); }
public object Create(ControllerContext controllerContext) { // Try to activate the controller using service provider var type = controllerContext.ActionDescriptor.ControllerTypeInfo.AsType(); var result = controllerContext.HttpContext.RequestServices.GetService(type); // Fallback to the default IControllerActivator if (result == null) { result = _controllerActivator.Create(controllerContext); } return(result); }
/// <inheritdoc /> public virtual object CreateController([NotNull] ActionContext actionContext) { var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor; if (actionDescriptor == null) { throw new ArgumentException( Resources.FormatActionDescriptorMustBeBasedOnControllerAction( typeof(ControllerActionDescriptor)), nameof(actionContext)); } var controllerType = actionDescriptor.ControllerTypeInfo.AsType(); var controller = _controllerActivator.Create(actionContext, controllerType); ActivateProperties(controller, actionContext); return(controller); }
/// <summary> /// Creates the specified request context. /// </summary> /// <param name="requestContext">The request context.</param> /// <param name="controllerType">Type of the controller.</param> /// <returns></returns> public override IController Create(RequestContext requestContext, Type controllerType) { if (controllerType == null) { return(null); } Type activatorType = ControllerActivatorRegistry.Matching(controllerType); IControllerActivator activator = activatorType != null ? (IControllerActivator)Container.GetServices(activatorType) : null; Controller controller = activator != null? activator.Create(requestContext, controllerType) as Controller: (GetControllerInstance(controllerType) ?? Activator.CreateInstance(controllerType)) as Controller; if (controller != null) { Type actionInvokerType; if (ActionInvokerRegistry.IsRegistered(controllerType)) { actionInvokerType = ActionInvokerRegistry.Matching(controllerType); } else { actionInvokerType = controller is IAsyncController ? KnownTypes.AsyncActionInvokerType : KnownTypes.SyncActionInvokerType; } IActionInvoker actionInvoker = Container.GetService(actionInvokerType) as IActionInvoker; if (actionInvoker != null) { controller.ActionInvoker = actionInvoker; } } return(controller); }
/// <inheritdoc /> public object CreateController(ControllerContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.ActionDescriptor == null) { throw new ArgumentException(Resources.FormatPropertyOfTypeCannotBeNull( nameof(ControllerContext.ActionDescriptor), nameof(ControllerContext))); } var controller = _controllerActivator.Create(context); foreach (var propertyActivator in _propertyActivators) { propertyActivator.Activate(context, controller); } return(controller); }
public object CreateController(ControllerContext context) => _propertyInjector.InjectProperties(_controllerActivator.Create(context));