/// <summary> /// 通过实现 System.Web.IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。 /// </summary> /// <param name="context">它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session和 Server)的引用。</param> public void ProcessRequest(HttpContext context) { WebRequestDecoder webRequestDecoder = WebRequestDecoder.CreateInstance(context); if (webRequestDecoder == null) { webRequestDecoder = new SimpleUrlDecoder(context); } string logicalMethod = webRequestDecoder.LogicalMethod; ResponseAnnotationAttribute methodAnnotation = this.GetMethodAnnotation(logicalMethod); WebResponseEncoder webResponseEncoder = WebResponseEncoder.CreateInstance(context, methodAnnotation.ResponseFormat); if (webResponseEncoder == null) { webResponseEncoder = new JQueryScriptEncoder(context); } try { this.Pre_Invoke(context); object srcObj = this.InvokeMethod(logicalMethod, webRequestDecoder.Deserialize()); webResponseEncoder.Write(srcObj); } catch (Exception ex) { string message = (ex.InnerException == null) ? ex.Message : ex.InnerException.Message; this.ResponseException(context, message, webResponseEncoder); } WebAjaxHandler.InitializeCachePolicy(context, methodAnnotation); }
public StringBuilder GetJavascript() { Type type = base.GetType(); Uri url = HttpContext.Current.Request.Url; string text = WebAjaxHandler.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.Query.Length > 0) ? url.ToString().Replace(url.Query, "") : url.ToString()); text = text.Replace("%CLS%", 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]; ResponseAnnotationAttribute methodAnnotation = this.GetMethodAnnotation(methodInfo.Name); stringBuilder.AppendLine("/*----------------------------"); stringBuilder.AppendLine(string.Format("功能说明:{0}", methodAnnotation.Description)); stringBuilder.AppendLine(string.Format("附加说明:缓存时间 {0} 秒", methodAnnotation.CacheDuration.ToString())); stringBuilder.AppendLine(string.Format(" 输出类型 {0}", methodAnnotation.ResponseFormat.ToString())); stringBuilder.AppendLine("-----------------------------*/"); string functionTemplete = WebAjaxHandler.GetFunctionTemplete(methodInfo, methodAnnotation.ResponseFormat); stringBuilder.AppendLine(functionTemplete); } return(stringBuilder); }
/// <summary> /// 参数解析 /// </summary> /// <param name="method">方法</param> /// <param name="args">参数</param> /// <returns>结果</returns> private object[] GetArguments(MethodInfo method, Dictionary <string, object> args) { List <object> list = new List <object>(); if (method != null) { ParameterInfo[] parameters = method.GetParameters(); ParameterInfo[] array = parameters; for (int i = 0; i < array.Length; i++) { ParameterInfo parameterInfo = array[i]; if (args != null && args.ContainsKey(parameterInfo.Name)) { object obj = args[parameterInfo.Name]; if (obj != null) { Type parameterType = WebAjaxHandler.GetParameterType(parameterInfo); if (obj is IDictionary <string, object> || obj is Array) { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); string input = javaScriptSerializer.Serialize(obj); list.Add(javaScriptSerializer.Deserialize(input, parameterType)); } else if (obj.GetType() != parameterType) { if (parameterType.IsEnum) { if (obj is int) { list.Add(Enum.ToObject(parameterType, obj)); } else { list.Add(Enum.Parse(parameterType, obj.ToString(), true)); } } else if (parameterType == typeof(Guid)) { list.Add(Guid.Parse(obj.ToString())); } else { list.Add(Convert.ChangeType(obj, parameterType)); } } else { list.Add(obj); } } } else { list.Add(WebAjaxHandler.IsNullAssignable(parameterInfo.ParameterType) ? null : parameterInfo.DefaultValue); } } } return(list.ToArray()); }
/// <summary> /// 获取参数类型 /// </summary> /// <param name="pi">ParameterInfo</param> /// <returns>Type</returns> private static Type GetParameterType(ParameterInfo pi) { return(WebAjaxHandler.IsNullable(pi.ParameterType) ? WebAjaxHandler.GetNullableInstanceType(pi.ParameterType) : pi.ParameterType); }
/// <summary> /// 判断空 /// </summary> /// <param name="type">type</param> /// <returns>bool</returns> private static bool IsNullAssignable(Type type) { return(!type.IsValueType || WebAjaxHandler.IsNullable(type)); }