예제 #1
0
        /// <summary>
        /// 获取请求方法特性
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <returns>特性对象</returns>
        private JsActionAttribute GetMethodAnnotation(string methodName)
        {
            MethodInfo method = base.GetType().GetMethod(methodName);

            JsActionAttribute[] array  = (JsActionAttribute[])method.GetCustomAttributes(typeof(JsActionAttribute), false);
            JsActionAttribute   result = null;

            if (array.Length > 0)
            {
                result = array[0];
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 获取Javascript
        /// </summary>
        /// <returns>视图结果</returns>
        public ActionResult GetJavascript()
        {
            //var macth = System.Text.RegularExpressions.Regex.Match(url, @"(\w+:\/\/)([^/:]+)(:\d*)?");
            //if (macth.Success)
            //{
            //    url = macth.Value;
            //}

            Type   type = this.GetType();
            Uri    uri  = Request.Url;
            string url  = uri.ToString();

            url = url.Substring(0, url.LastIndexOf("/"));
            url = url.Substring(0, url.LastIndexOf("/"));

            string text = GetScriptTemplete();

            text = text.Replace("$H_DES$", "通过jQuery.ajax完成服务端函数调用");
            text = text.Replace("$H_DATE$", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            text = text.Replace("$URL$", url);
            text = text.Replace("$CLS$", GetControllerName(type.Name));
            StringBuilder stringBuilder = new StringBuilder(text);

            MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            MethodInfo[] array   = methods;
            for (int i = 0; i < array.Length; i++)
            {
                MethodInfo        methodInfo       = array[i];
                JsActionAttribute methodAnnotation = this.GetMethodAnnotation(methodInfo.Name);
                if (methodAnnotation != null && methodAnnotation.IsCreateJs)
                {
                    string functionTemplete = GetFunctionTemplete(methodInfo);
                    stringBuilder.AppendLine(functionTemplete);
                }
            }
            return(JavaScript(stringBuilder.ToString()));
        }