public object[] GetMultiObjectsFormString(string input, ActionDescription action)
        {
            Dictionary<string, object> dict = jss.DeserializeObject(input) as Dictionary<string, object>;

            //if( dict.Count != action.Parameters.Length )
            //    throw new ArgumentException("客户端提交的数据项与服务端的参数项的数量不匹配。");

            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                string name = action.Parameters[i].Name;
                object value = (from kv in dict
                                where string.Compare(kv.Key, name, StringComparison.OrdinalIgnoreCase) == 0
                                select kv.Value).FirstOrDefault();

                if (value != null)
                {
                    Type destType = action.Parameters[i].ParameterType.GetRealType();

                    MethodInfo method = s_methodConvertToType.MakeGenericMethod(destType);
                    object parameter = method.FastInvoke(jss, new object[] { value });
                    parameters[i] = parameter;
                }
            }

            return parameters;
        }
Example #2
0
        public object[] GetMultiObjectsFormString(string input, ActionDescription action)
        {
            Dictionary <string, object> dict = jss.DeserializeObject(input) as Dictionary <string, object>;

            //if( dict.Count != action.Parameters.Length )
            //    throw new ArgumentException("客户端提交的数据项与服务端的参数项的数量不匹配。");

            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                string name  = action.Parameters[i].Name;
                object value = (from kv in dict
                                where string.Compare(kv.Key, name, StringComparison.OrdinalIgnoreCase) == 0
                                select kv.Value).FirstOrDefault();

                if (value != null)
                {
                    Type destType = action.Parameters[i].ParameterType.GetRealType();

                    MethodInfo method    = s_methodConvertToType.MakeGenericMethod(destType);
                    object     parameter = method.FastInvoke(jss, new object[] { value });
                    parameters[i] = parameter;
                }
            }

            return(parameters);
        }
        /// <summary>
        /// 根据一个页面请求路径,返回内部表示的调用信息。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static InvokeInfo GetActionInvokeInfo(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            ActionDescription action = null;

            if (s_PageActionDict.TryGetValue(url, out action) == false)
            {
                return(null);
            }

            InvokeInfo vkInfo = new InvokeInfo();

            vkInfo.Controller = action.PageController;
            vkInfo.Action     = action;

            if (vkInfo.Action.MethodInfo.IsStatic == false)
            {
                //vkInfo.Instance = Activator.CreateInstance(vkInfo.Controller.ControllerType);
                vkInfo.Instance = vkInfo.Controller.ControllerType.FastNew();
            }

            return(vkInfo);
        }
Example #4
0
        public object GetObjectFromString(string input, ActionDescription action)
        {
            Type destType = action.Parameters[0].ParameterType.GetRealType();

            MethodInfo deserialize = s_methodDeserialize.MakeGenericMethod(destType);

            return(deserialize.FastInvoke(jss, new object[] { input }));
        }
        public object GetObjectFromString(string input, ActionDescription action)
        {
            Type destType = action.Parameters[0].ParameterType.GetRealType();

            MethodInfo deserialize = s_methodDeserialize.MakeGenericMethod(destType);

            return deserialize.FastInvoke(jss, new object[] { input });
        }
 public object[] GetParameters(HttpRequest request, ActionDescription action)
 {
     if (action.Parameters.Length == 1)
     {
         object value = GetObjectFromRequest(request, action);
         return new object[1] { value };
     }
     else
         return GetMultiObjectsFormRequest(request, action);
 }
        public object GetObjectFromRequest(HttpRequest request, ActionDescription action)
        {
            Type destType = action.Parameters[0].ParameterType.GetRealType();

            XmlSerializer mySerializer = new XmlSerializer(destType);

            request.InputStream.Position = 0;
            StreamReader sr = new StreamReader(request.InputStream, request.ContentEncoding);
            return mySerializer.Deserialize(sr);
        }
Example #8
0
        public object GetObjectFromRequest(HttpRequest request, ActionDescription action)
        {
            Type destType = action.Parameters[0].ParameterType.GetRealType();

            XmlSerializer mySerializer = new XmlSerializer(destType);

            request.InputStream.Position = 0;
            StreamReader sr = new StreamReader(request.InputStream, request.ContentEncoding);

            return(mySerializer.Deserialize(sr));
        }
        public object[] GetParameters(HttpRequest request, ActionDescription action)
        {
            string input = request.ReadInputStream();

            if (action.Parameters.Length == 1)
            {
                object value = GetObjectFromString(input, action);
                return new object[1] { value };
            }
            else
                return GetMultiObjectsFormString(input, action);
        }
Example #10
0
 public object[] GetParameters(HttpRequest request, ActionDescription action)
 {
     if (action.Parameters.Length == 1)
     {
         object value = GetObjectFromRequest(request, action);
         return(new object[1] {
             value
         });
     }
     else
     {
         return(GetMultiObjectsFormRequest(request, action));
     }
 }
        /// <summary>
        /// 根据要调用的方法名返回对应的 Action
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static ActionDescription GetServiceAction(Type controller, string action, HttpRequest request)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (string.IsNullOrEmpty(action))
            {
                throw new ArgumentNullException("action");
            }

            // 首先尝试从缓存中读取
            string            key = request.HttpMethod + "#" + controller.FullName + "@" + action;
            ActionDescription mi  = (ActionDescription)s_ServiceActionTable[key];

            if (mi == null)
            {
                bool saveToCache = true;

                MethodInfo method = FindAction(action, controller, request);

                if (method == null)
                {
                    // 如果Action的名字是submit并且是POST提交,则需要自动寻找Action
                    // 例如:多个提交都采用一样的方式:POST /Ajax/Product/submit
                    if (action.IsSame("submit") && request.HttpMethod.IsSame("POST"))
                    {
                        // 自动寻找Action
                        method      = FindSubmitAction(controller, request);
                        saveToCache = false;
                    }
                }

                if (method == null)
                {
                    return(null);
                }

                var attr = method.GetMyAttribute <ActionAttribute>();
                mi = new ActionDescription(method, attr);

                if (saveToCache)
                {
                    s_ServiceActionTable[key] = mi;
                }
            }

            return(mi);
        }
Example #12
0
        public object[] GetParameters(HttpRequest request, ActionDescription action)
        {
            string input = request.ReadInputStream();

            if (action.Parameters.Length == 1)
            {
                object value = GetObjectFromString(input, action);
                return(new object[1] {
                    value
                });
            }
            else
            {
                return(GetMultiObjectsFormString(input, action));
            }
        }
Example #13
0
        public object[] GetMultiObjectsFormRequest(HttpRequest request, ActionDescription action)
        {
            string xml = request.ReadInputStream();

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            XmlNode root = doc.LastChild;

            //if( root.ChildNodes.Count != action.Parameters.Length )
            //    throw new ArgumentException("客户端提交的数据项与服务端的参数项的数量不匹配。");

            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                string  name = action.Parameters[i].Name;
                XmlNode node = (from n in root.ChildNodes.Cast <XmlNode>()
                                where string.Compare(n.Name, name, StringComparison.OrdinalIgnoreCase) == 0
                                select n).FirstOrDefault();

                if (node != null)
                {
                    object parameter = null;
                    Type   destType  = action.Parameters[i].ParameterType.GetRealType();

                    if (destType.IsSupportableType())
                    {
                        parameter = ModelHelper.SafeChangeType(node.InnerText, destType);
                    }
                    else
                    {
                        parameter = XmlDeserialize(node.OuterXml, destType, request.ContentEncoding);
                    }

                    parameters[i] = parameter;
                }
            }

            return(parameters);
        }
Example #14
0
        public object[] GetMultiObjectsFormRequest(HttpRequest request, ActionDescription action)
        {
            string xml = request.ReadInputStream();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNode root = doc.LastChild;

            //if( root.ChildNodes.Count != action.Parameters.Length )
            //    throw new ArgumentException("客户端提交的数据项与服务端的参数项的数量不匹配。");

            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                string name = action.Parameters[i].Name;
                XmlNode node = (from n in root.ChildNodes.Cast<XmlNode>()
                                where string.Compare(n.Name, name, StringComparison.OrdinalIgnoreCase) == 0
                                select n).FirstOrDefault();

                if (node != null)
                {
                    object parameter = null;
                    Type destType = action.Parameters[i].ParameterType.GetRealType();

                    if (destType.IsSupportableType())
                        parameter = ModelHelper.SafeChangeType(node.InnerText, destType);
                    else
                        parameter = XmlDeserialize(node.OuterXml, destType, request.ContentEncoding);

                    parameters[i] = parameter;
                }
            }

            return parameters;
        }
        public object[] GetParameters(HttpRequest request, ActionDescription action)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }


            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < action.Parameters.Length; i++)
            {
                ParameterInfo p = action.Parameters[i];

                if (p.IsOut)
                {
                    continue;
                }

                if (p.ParameterType == typeof(VoidType))
                {
                    continue;
                }

                if (p.ParameterType == typeof(NameValueCollection))
                {
                    if (string.Compare(p.Name, "Form", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        parameters[i] = request.Form;
                    }
                    else if (string.Compare(p.Name, "QueryString", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        parameters[i] = request.QueryString;
                    }
                    else if (string.Compare(p.Name, "Headers", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        parameters[i] = request.Headers;
                    }
                    else if (string.Compare(p.Name, "ServerVariables", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        parameters[i] = request.ServerVariables;
                    }
                }
                else
                {
                    Type paramterType = p.ParameterType.GetRealType();

                    // 如果参数是可支持的类型,则直接从HttpRequest中读取并赋值
                    if (paramterType.IsSupportableType())
                    {
                        object val = ModelHelper.GetValueByNameAndTypeFrommRequest(request, p.Name, paramterType, null);
                        if (val != null)
                        {
                            parameters[i] = val;
                        }
                        else
                        {
                            if (p.ParameterType.IsValueType && p.ParameterType.IsNullableType() == false)
                            {
                                throw new ArgumentException("未能找到指定的参数值:" + p.Name);
                            }
                        }
                    }
                    else
                    {
                        // 自定义的类型。首先创建实例,然后给所有成员赋值。
                        // 注意:这里不支持嵌套类型的自定义类型。
                        //object item = Activator.CreateInstance(paramterType);
                        object item = paramterType.FastNew();
                        ModelHelper.FillModel(request, item, p.Name);
                        parameters[i] = item;
                    }
                }
            }

            return(parameters);
        }
        /// <summary>
        /// 根据要调用的方法名返回对应的 Action 
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static ActionDescription GetServiceAction(Type controller, string action, HttpRequest request)
        {
            if (controller == null)
                throw new ArgumentNullException("controller");
            if (string.IsNullOrEmpty(action))
                throw new ArgumentNullException("action");

            // 首先尝试从缓存中读取
            string key = request.HttpMethod + "#" + controller.FullName + "@" + action;
            ActionDescription mi = (ActionDescription)s_ServiceActionTable[key];

            if (mi == null)
            {
                bool saveToCache = true;

                MethodInfo method = FindAction(action, controller, request);

                if (method == null)
                {
                    // 如果Action的名字是submit并且是POST提交,则需要自动寻找Action
                    // 例如:多个提交都采用一样的方式:POST /Ajax/Product/submit
                    if (action.IsSame("submit") && request.HttpMethod.IsSame("POST"))
                    {
                        // 自动寻找Action
                        method = FindSubmitAction(controller, request);
                        saveToCache = false;
                    }
                }

                if (method == null)
                    return null;

                var attr = method.GetMyAttribute<ActionAttribute>();
                mi = new ActionDescription(method, attr);

                if (saveToCache)
                    s_ServiceActionTable[key] = mi;
            }

            return mi;
        }
        /// <summary>
        /// 加载所有的Controller
        /// </summary>
        private static void InitControllers()
        {
            BaseActionHandlerFactory[] baseActionHandlerFactoryList = GetConfigBaseActionHandlerFactory();

            List<ControllerDescription> serviceControllerList = new List<ControllerDescription>(1024);
            var pageControllerList = new List<ControllerDescription>(1024);

            ICollection assemblies = BuildManager.GetReferencedAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                // 过滤以【System】开头的程序集,加快速度
                if (assembly.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase))
                    continue;

                foreach (Type t in assembly.GetExportedTypes())
                {
                    if (t.IsClass == false)
                        continue;

                    if (t.Name.EndsWith("Controller"))
                        pageControllerList.Add(new ControllerDescription(t));

                    for (int i = 0; i < baseActionHandlerFactoryList.Length; i++)
                        if (baseActionHandlerFactoryList[i].TypeIsService(t))
                        {
                            serviceControllerList.Add(new ControllerDescription(t));
                            break;
                        }

                }
            }

            // 用于Ajax or Service 调用的Action信息则采用延迟加载的方式。

            s_ServiceFullNameDict = serviceControllerList.ToDictionary(x => x.ControllerType.FullName, StringComparer.OrdinalIgnoreCase);

            s_ServiceShortNameDict = new Dictionary<string, ControllerDescription>(s_ServiceFullNameDict.Count, StringComparer.OrdinalIgnoreCase);
            foreach (ControllerDescription description in serviceControllerList)
            {
                try
                {
                    s_ServiceShortNameDict.Add(description.ControllerType.Name, description);
                }
                catch (ArgumentException)
                {
                    // 如果遇到已存在的KEY,把原先存放的项也设置为 null,便于查找时返回 null
                    s_ServiceShortNameDict[description.ControllerType.Name] = null;
                }
            }



            // 提前加载Page Controller中的所有Action方法
            s_PageActionDict = new Dictionary<string, ActionDescription>(4096, StringComparer.OrdinalIgnoreCase);

            foreach (ControllerDescription controller in pageControllerList)
            {
                foreach (MethodInfo m in controller.ControllerType.GetMethods(ActionBindingFlags))
                {
                    PageUrlAttribute[] pageUrlAttrs = m.GetMyAttributes<PageUrlAttribute>();
                    ActionAttribute actionAttr = m.GetMyAttribute<ActionAttribute>();

                    if (pageUrlAttrs.Length > 0 && actionAttr != null)
                    {
                        ActionDescription actionDescription =
                            new ActionDescription(m, actionAttr) { PageController = controller };

                        foreach (PageUrlAttribute attr in pageUrlAttrs)
                        {
                            if (string.IsNullOrEmpty(attr.Url) == false)
                                s_PageActionDict.Add(attr.Url, actionDescription);
                        }
                    }
                }
            }


        }
Example #18
0
        private static object[] GetActionCallParameters(HttpContext context, ActionDescription action)
        {
            if (action.Parameters == null || action.Parameters.Length == 0)
                return null;

            IActionParametersProvider provider = ActionParametersProviderFactory.CreateActionParametersProvider(context.Request);
            return provider.GetParameters(context.Request, action);
        }
        public object[] GetParameters(HttpRequest request, ActionDescription action)
        {
            if (request == null)
                throw new ArgumentNullException("request");
            if (action == null)
                throw new ArgumentNullException("action");


            object[] parameters = new object[action.Parameters.Length];

            for (int i = 0; i < action.Parameters.Length; i++)
            {
                ParameterInfo p = action.Parameters[i];

                if (p.IsOut)
                    continue;

                if (p.ParameterType == typeof(VoidType))
                    continue;

                if (p.ParameterType == typeof(NameValueCollection))
                {
                    if (string.Compare(p.Name, "Form", StringComparison.OrdinalIgnoreCase) == 0)
                        parameters[i] = request.Form;
                    else if (string.Compare(p.Name, "QueryString", StringComparison.OrdinalIgnoreCase) == 0)
                        parameters[i] = request.QueryString;
                    else if (string.Compare(p.Name, "Headers", StringComparison.OrdinalIgnoreCase) == 0)
                        parameters[i] = request.Headers;
                    else if (string.Compare(p.Name, "ServerVariables", StringComparison.OrdinalIgnoreCase) == 0)
                        parameters[i] = request.ServerVariables;
                }
                else
                {
                    Type paramterType = p.ParameterType.GetRealType();

                    // 如果参数是可支持的类型,则直接从HttpRequest中读取并赋值
                    if (paramterType.IsSupportableType())
                    {
                        object val = ModelHelper.GetValueByNameAndTypeFrommRequest(request, p.Name, paramterType, null);
                        if (val != null)
                            parameters[i] = val;
                        else
                        {
                            if (p.ParameterType.IsValueType && p.ParameterType.IsNullableType() == false)
                                throw new ArgumentException("未能找到指定的参数值:" + p.Name);
                        }
                    }
                    else
                    {
                        // 自定义的类型。首先创建实例,然后给所有成员赋值。
                        // 注意:这里不支持嵌套类型的自定义类型。
                        //object item = Activator.CreateInstance(paramterType);
                        object item = paramterType.FastNew();
                        ModelHelper.FillModel(request, item, p.Name);
                        parameters[i] = item;
                    }
                }
            }

            return parameters;
        }
        /// <summary>
        /// 加载所有的Controller
        /// </summary>
        private static void InitControllers()
        {
            BaseActionHandlerFactory[] baseActionHandlerFactoryList = GetConfigBaseActionHandlerFactory();

            List <ControllerDescription> serviceControllerList = new List <ControllerDescription>(1024);
            var pageControllerList = new List <ControllerDescription>(1024);

            ICollection assemblies = BuildManager.GetReferencedAssemblies();

            foreach (Assembly assembly in assemblies)
            {
                // 过滤以【System】开头的程序集,加快速度
                if (assembly.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                foreach (Type t in assembly.GetExportedTypes())
                {
                    if (t.IsClass == false)
                    {
                        continue;
                    }

                    if (t.Name.EndsWith("Controller"))
                    {
                        pageControllerList.Add(new ControllerDescription(t));
                    }

                    for (int i = 0; i < baseActionHandlerFactoryList.Length; i++)
                    {
                        if (baseActionHandlerFactoryList[i].TypeIsService(t))
                        {
                            serviceControllerList.Add(new ControllerDescription(t));
                            break;
                        }
                    }
                }
            }

            // 用于Ajax or Service 调用的Action信息则采用延迟加载的方式。

            s_ServiceFullNameDict = serviceControllerList.ToDictionary(x => x.ControllerType.FullName, StringComparer.OrdinalIgnoreCase);

            s_ServiceShortNameDict = new Dictionary <string, ControllerDescription>(s_ServiceFullNameDict.Count, StringComparer.OrdinalIgnoreCase);
            foreach (ControllerDescription description in serviceControllerList)
            {
                try
                {
                    s_ServiceShortNameDict.Add(description.ControllerType.Name, description);
                }
                catch (ArgumentException)
                {
                    // 如果遇到已存在的KEY,把原先存放的项也设置为 null,便于查找时返回 null
                    s_ServiceShortNameDict[description.ControllerType.Name] = null;
                }
            }



            // 提前加载Page Controller中的所有Action方法
            s_PageActionDict = new Dictionary <string, ActionDescription>(4096, StringComparer.OrdinalIgnoreCase);

            foreach (ControllerDescription controller in pageControllerList)
            {
                foreach (MethodInfo m in controller.ControllerType.GetMethods(ActionBindingFlags))
                {
                    PageUrlAttribute[] pageUrlAttrs = m.GetMyAttributes <PageUrlAttribute>();
                    ActionAttribute    actionAttr   = m.GetMyAttribute <ActionAttribute>();

                    if (pageUrlAttrs.Length > 0 && actionAttr != null)
                    {
                        ActionDescription actionDescription =
                            new ActionDescription(m, actionAttr)
                        {
                            PageController = controller
                        };

                        foreach (PageUrlAttribute attr in pageUrlAttrs)
                        {
                            if (string.IsNullOrEmpty(attr.Url) == false)
                            {
                                s_PageActionDict.Add(attr.Url, actionDescription);
                            }
                        }
                    }
                }
            }
        }