コード例 #1
0
        public DirectMethod GetMethod(string actionName, string methodName)
        {
            DirectMethod method = null;
            DirectAction action = GetAction(actionName);

            if (action != null)
            {
                method = action.GetMethod(methodName);
            }
            return(method);
        }
コード例 #2
0
        private void ExecuteRequest(RequestContext requestContext, DirectRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request", DirectResources.Common_DirectRequestIsNull);
            }

            HttpContextBase httpContext = requestContext.HttpContext;
            RouteData       routeData   = requestContext.RouteData;

            routeData.Values["controller"] = request.Action;
            routeData.Values["action"]     = request.Method;
            httpContext.Items[DirectRequest.DirectRequestKey] = request;
            var controller = (Controller)_factory.CreateController(requestContext, request.Action);

            DirectAction action = GetAction(request.Action);

            if (action == null)
            {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_ActionNotFound, request.Action));
            }

            DirectMethod method = action.GetMethod(request.Method);

            if (method == null)
            {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_MethodNotFound, request.Method, action.Name));
            }

            if (!method.IsFormHandler && method.Params == null)
            {
                if (request.Data == null && method.Len > 0 || request.Data != null && request.Data.Length != method.Len)
                {
                    throw new ArgumentException(String.Format(DirectResources.DirectProvider_WrongNumberOfArguments, request.Method, request.Action));
                }
            }

            try {
                controller.ActionInvoker = new DirectMethodInvoker();
                (controller as IController).Execute(requestContext);
            } catch (DirectException exception) {
                var errorResponse = new DirectErrorResponse(request, exception);
                errorResponse.Write(httpContext.Response);
            } finally {
                _factory.ReleaseController(controller);
            }

            httpContext.Items.Remove(DirectRequest.DirectRequestKey);
        }
コード例 #3
0
        private void Configure(Type type) {
            var methods = type.GetMethods()
                .Where(x =>
                    x.IsPublic &&
                    (x.ReturnType == typeof(ActionResult) || x.ReturnType.IsSubclassOf(typeof(ActionResult))) &&
                    !x.HasAttribute<DirectIgnoreAttribute>()
                );

            foreach (MethodInfo method in methods) {
                var directMethod = new DirectMethod(method);
                if (_methods.ContainsKey(directMethod.Name)) {
                    throw new Exception(String.Format(DirectResources.DirectAction_MethodExists, directMethod.Name, Name));
                }
                _methods.Add(directMethod.Name, directMethod);
            }
        }
コード例 #4
0
        private void Configure(Type type)
        {
            var methods = type.GetMethods()
                          .Where(x =>
                                 x.IsPublic &&
                                 (x.ReturnType == typeof(ActionResult) || x.ReturnType.IsSubclassOf(typeof(ActionResult))) &&
                                 !x.HasAttribute <DirectIgnoreAttribute>()
                                 );

            foreach (MethodInfo method in methods)
            {
                var directMethod = new DirectMethod(method);
                if (_methods.ContainsKey(directMethod.Name))
                {
                    throw new Exception(String.Format(DirectResources.DirectAction_MethodExists, directMethod.Name, Name));
                }
                _methods.Add(directMethod.Name, directMethod);
            }
        }
コード例 #5
0
        public DirectValueProvider(DirectRequest directRequest, ParameterDescriptor[] parameterDescriptors)
        {
            int paramCount = parameterDescriptors.Length;

            Object[] data = directRequest.Data;

            DirectMethod directMethod       = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method);
            bool         usesNamedArguments = (directMethod != null && directMethod.UsesNamedArguments);

            if (paramCount > 0)
            {
                if (usesNamedArguments)   // named arguments. match params by name
                {
                    var dataObj = data[0] as JObject;

                    for (int i = 0; i < paramCount; i++)
                    {
                        string pName = parameterDescriptors[i].ParameterName;
                        Type   pType = parameterDescriptors[i].ParameterType;
                        JToken value = dataObj != null?dataObj.SelectToken(pName) : null;

                        object rawValue = null;

                        if (value != null && value.Type != JTokenType.Null && value.Type != JTokenType.Undefined)
                        {
                            Type vType = value.GetType();
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray))
                            {
                                rawValue = JsonConvert.DeserializeObject(value.ToString(), pType);
                            }
                            else
                            {
                                rawValue = Convert.ChangeType(value.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(pName, new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                }
                else
                {
                    for (int i = 0; i < parameterDescriptors.Length; i++)
                    {
                        object rawValue = data[i];

                        if (rawValue != null)
                        {
                            Type vType = rawValue.GetType();
                            Type pType = parameterDescriptors[i].ParameterType;

                            // Deserialize only objects and arrays and let MVC handle everything else.
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray))
                            {
                                rawValue = JsonConvert.DeserializeObject(rawValue.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(parameterDescriptors[i].ParameterName,
                                    new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                }
            }
        }