public void Invalid_ServiceWithAction_Names() { Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("task")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("task_service")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("task_service/create")); }
/// <summary> /// This class is called by Web API system to select action method from given controller. /// </summary> /// <param name="controllerContext">Controller context</param> /// <returns>Action to be used</returns> public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { object controllerInfoObj; if (controllerContext.ControllerDescriptor.Properties.TryGetValue("__AbpDynamicApiControllerInfo", out controllerInfoObj)) { //Get controller information which is selected by AbpHttpControllerSelector. var controllerInfo = controllerInfoObj as DynamicApiControllerInfo; if (controllerInfo == null) { throw new AbpException("__AbpDynamicApiControllerInfo in ControllerDescriptor.Properties is not a " + typeof(DynamicApiControllerInfo).FullName + " class."); } //Get action name var serviceNameWithAction = (controllerContext.RouteData.Values["serviceNameWithAction"] as string); if (serviceNameWithAction != null) { var actionName = DynamicApiServiceNameHelper.GetActionNameInServiceNameWithAction(serviceNameWithAction); //Get action information if (!controllerInfo.Actions.ContainsKey(actionName)) { throw new AbpException("There is no action " + actionName + " defined for api controller " + controllerInfo.ServiceName); } return(new DyanamicHttpActionDescriptor(controllerContext.ControllerDescriptor, controllerInfo.Actions[actionName].Method, controllerInfo.Actions[actionName].Filters)); } } return(base.SelectAction(controllerContext)); }
/// <summary> /// 此方法被系统调用,选择给定Controller的Action方法 /// </summary> /// <param name="controllerContext">Controller上下文</param> /// <returns></returns> public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { object controllerInfoObj; if (!controllerContext.ControllerDescriptor.Properties.TryGetValue("__MSDynamicApiControllerInfo", out controllerInfoObj)) { return(base.SelectAction(controllerContext)); } var controllerInfo = controllerInfoObj as DynamicApiControllerInfo; if (null == controllerInfo) { throw new MSException("__MSDynamicApiControllerInfo in ControllerDescriptor.Properties is not a " + typeof(DynamicApiControllerInfo).FullName + " class."); } var hasActionName = (bool)controllerContext.ControllerDescriptor.Properties["__MSDynamicApiHasActionName"]; if (!hasActionName) { return(GetActionDescriptorByCurrentHttpVerb(controllerContext, controllerInfo)); } //从路由中获取action名称 var serviceNameWithAction = controllerContext.RouteData.Values["serviceNameWithAction"] as string; if (null == serviceNameWithAction) { return(base.SelectAction(controllerContext)); } var actionName = DynamicApiServiceNameHelper.GetActionNameInServiceNameWithAction(serviceNameWithAction); return(GetActionDescriptorByActionName(controllerContext, controllerInfo, actionName)); }
/// <summary> /// This method is called by Web API system to select the controller for this request. /// </summary> /// <param name="request">Request object</param> /// <returns>The controller to be used</returns> public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { //Get request and route data if (request == null) { return(base.SelectController(null)); } var routeData = request.GetRouteData(); if (routeData == null) { return(base.SelectController(request)); } //Get serviceNameWithAction from route string serviceNameWithAction; if (!routeData.Values.TryGetValue("serviceNameWithAction", out serviceNameWithAction)) { return(base.SelectController(request)); } //Normalize serviceNameWithAction if (serviceNameWithAction.EndsWith("/")) { serviceNameWithAction = serviceNameWithAction.Substring(0, serviceNameWithAction.Length - 1); routeData.Values["serviceNameWithAction"] = serviceNameWithAction; } //Get the dynamic controller var hasActionName = false; var controllerInfo = _dynamicApiControllerManager.FindOrNull(serviceNameWithAction); if (controllerInfo == null) { if (!DynamicApiServiceNameHelper.IsValidServiceNameWithAction(serviceNameWithAction)) { return(base.SelectController(request)); } var serviceName = DynamicApiServiceNameHelper.GetServiceNameInServiceNameWithAction(serviceNameWithAction); controllerInfo = _dynamicApiControllerManager.FindOrNull(serviceName); if (controllerInfo == null) { return(base.SelectController(request)); } hasActionName = true; } //Create the controller descriptor var controllerDescriptor = new DynamicHttpControllerDescriptor(_configuration, controllerInfo); controllerDescriptor.Properties["__AbpDynamicApiControllerInfo"] = controllerInfo; controllerDescriptor.Properties["__AbpDynamicApiHasActionName"] = hasActionName; return(controllerDescriptor); }
public void Valid_Service_Names() { Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/task")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/tasks/task")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/taskService")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/tasks/taskService")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/tasks/task_service")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/myNameSpace1/MyNameSpace2/mynamespace3/myserviceName")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceName("taskever/myName_Space1/MyName_Space2/mynamespace_3/myservice")); }
public void Invalid_Service_Names() { Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("task")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("task_service")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("taskever/task service")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("taskever/123task_service")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName(" taskever/task_service")); Assert.Equal(false, DynamicApiServiceNameHelper.IsValidServiceName("taskever/ task_service")); }
public void Valid_Service_WithAction_Names() { Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/task/create")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/tasks/task/update")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/taskService/delete")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/tasks/taskService/getAllTasks")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/tasks/task_service/deleteTask")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/myNameSpace1/MyNameSpace2/mynamespace3/myserviceName/CreateNew")); Assert.Equal(true, DynamicApiServiceNameHelper.IsValidServiceNameWithAction("taskever/myName_Space1/MyName_Space2/mynamespace_3/myservice/test_action_Name")); }
/// <summary> /// 该方法将被系统调用,根据请求选择合适的Controller /// </summary> /// <param name="request"></param> /// <returns></returns> public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { if (null == request) { return(base.SelectController(null)); } var routeData = request.GetRouteData(); if (null == routeData) { return(base.SelectController(request)); } string serviceNameWithAction; if (!routeData.Values.TryGetValue("serviceNameWithAction", out serviceNameWithAction)) { return(base.SelectController(request)); } if (Convert.ToString(serviceNameWithAction).EndsWith("/")) { serviceNameWithAction = serviceNameWithAction.Substring(0, serviceNameWithAction.Length - 1); routeData.Values["serviceNameWithAction"] = serviceNameWithAction; } var hasActionName = false; var controllerInfo = _dynamicApiControllerManager.FindOrNull(serviceNameWithAction); if (null == controllerInfo) { if (!DynamicApiServiceNameHelper.IsValidServiceNameWithAction(serviceNameWithAction)) { return(base.SelectController(request)); } var serviceName = DynamicApiServiceNameHelper.GetServiceNameInServiceNameWithAction(serviceNameWithAction); controllerInfo = _dynamicApiControllerManager.FindOrNull(serviceName); if (null == controllerInfo) { return(base.SelectController(request)); } hasActionName = true; } // 创建并返回Controller描述器 var controllerDescriptor = new DynamicHttpControllerDescriptor(_configuration, controllerInfo); controllerDescriptor.Properties["__MSDynamicApiControllerInfo"] = controllerInfo; controllerDescriptor.Properties["__MSDynamicApiHasActionName"] = hasActionName; return(controllerDescriptor); }
/// <summary> /// Creates a new instance of ApiControllerInfoBuilder. /// </summary> /// <param name="serviceName">Name of the controller</param> public ApiControllerBuilder(string serviceName) { if (string.IsNullOrWhiteSpace(serviceName)) { throw new ArgumentException("serviceName null or empty!", "serviceName"); } if (!DynamicApiServiceNameHelper.IsValidServiceName(serviceName)) { throw new ArgumentException("serviceName is not properly formatted! It must contain a single-depth namespace at least! For example: 'myapplication/myservice'.", "serviceName"); } _serviceName = serviceName; _actionBuilders = new Dictionary <string, ApiControllerActionBuilder <T> >(); foreach (var methodInfo in DynamicApiControllerActionHelper.GetMethodsOfType(typeof(T))) { _actionBuilders[methodInfo.Name] = new ApiControllerActionBuilder <T>(this, methodInfo); } }
/// <summary> /// This class is called by Web API system to select action method from given controller. /// </summary> /// <param name="controllerContext">Controller context</param> /// <returns>Action to be used</returns> public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { object controllerInfoObj; if (!controllerContext.ControllerDescriptor.Properties.TryGetValue("__AbpDynamicApiControllerInfo", out controllerInfoObj)) { return(GetDefaultActionDescriptor(controllerContext)); } //Get controller information which is selected by AbpHttpControllerSelector. var controllerInfo = controllerInfoObj as DynamicApiControllerInfo; if (controllerInfo == null) { throw new AbpException("__AbpDynamicApiControllerInfo in ControllerDescriptor.Properties is not a " + typeof(DynamicApiControllerInfo).FullName + " class."); } //No action name case var hasActionName = (bool)controllerContext.ControllerDescriptor.Properties["__AbpDynamicApiHasActionName"]; if (!hasActionName) { return(GetActionDescriptorByCurrentHttpVerb(controllerContext, controllerInfo)); } //Get action name from route var serviceNameWithAction = (controllerContext.RouteData.Values["serviceNameWithAction"] as string); if (serviceNameWithAction == null) { return(GetDefaultActionDescriptor(controllerContext)); } var actionName = DynamicApiServiceNameHelper.GetActionNameInServiceNameWithAction(serviceNameWithAction); return(GetActionDescriptorByActionName( controllerContext, controllerInfo, actionName )); }
/// <summary> /// Creates a new instance of ApiControllerInfoBuilder. /// </summary> /// <param name="serviceName">Name of the controller</param> /// <param name="iocResolver">Ioc resolver</param> public DefaultControllerBuilder(string serviceName, IIocResolver iocResolver, IControllerRegister defaultControllerManager) { Check.NotNull(iocResolver, nameof(iocResolver)); if (string.IsNullOrWhiteSpace(serviceName)) { throw new ArgumentException("serviceName null or empty!", "serviceName"); } if (!DynamicApiServiceNameHelper.IsValidServiceName(serviceName)) { throw new ArgumentException("serviceName is not properly formatted! It must contain a single-depth namespace at least! For example: 'myapplication/myservice'.", "serviceName"); } _iocResolver = iocResolver; _defaultControllerManager = defaultControllerManager; ServiceName = serviceName; ServiceInterfaceType = typeof(T); _actionBuilders = new Dictionary <string, TControllerActionBuilder>(); var methodInfos = DynamicApiControllerActionHelper.GetMethodsOfType(typeof(T)) .Where(methodInfo => methodInfo.GetSingleAttributeOrNull <BlocksActionNameAttribute>() != null); foreach (var methodInfo in methodInfos) { var actionBuilder = (TControllerActionBuilder)typeof(TControllerActionBuilder).New(this, methodInfo, iocResolver); var remoteServiceAttr = methodInfo.GetSingleAttributeOrNull <RemoteServiceAttribute>(); if (remoteServiceAttr != null && !remoteServiceAttr.IsEnabledFor(methodInfo)) { actionBuilder.DontCreateAction(); } var actionNameAttr = methodInfo.GetSingleAttributeOrNull <BlocksActionNameAttribute>(); _actionBuilders[actionNameAttr.ActionName] = actionBuilder; } }
/// <summary> /// This method is called by Web API system to select the controller for this request. /// </summary> /// <param name="request">Request object</param> /// <returns>The controller to be used</returns> public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { if (request != null) { var routeData = request.GetRouteData(); if (routeData != null) { string serviceNameWithAction; if (routeData.Values.TryGetValue("serviceNameWithAction", out serviceNameWithAction) && DynamicApiServiceNameHelper.IsValidServiceNameWithAction(serviceNameWithAction)) { var serviceName = DynamicApiServiceNameHelper.GetServiceNameInServiceNameWithAction(serviceNameWithAction); var controllerInfo = DynamicApiControllerManager.FindOrNull(serviceName); if (controllerInfo != null) { var controllerDescriptor = new DynamicHttpControllerDescriptor(_configuration, controllerInfo.ServiceName, controllerInfo.Type, controllerInfo.Filters); controllerDescriptor.Properties["__AbpDynamicApiControllerInfo"] = controllerInfo; return(controllerDescriptor); } } } } return(base.SelectController(request)); }