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> /// 通过实现 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); }
/// <summary> /// 获取请求方法特性 /// </summary> /// <param name="methodName">方法名</param> /// <returns>特性对象</returns> private ResponseAnnotationAttribute GetMethodAnnotation(string methodName) { MethodInfo method = base.GetType().GetMethod(methodName); ResponseAnnotationAttribute[] array = (ResponseAnnotationAttribute[])method.GetCustomAttributes(typeof(ResponseAnnotationAttribute), false); ResponseAnnotationAttribute result = ResponseAnnotationAttribute.Default; if (array.Length > 0) { result = array[0]; } return(result); }
/// <summary> /// 初始化缓存池 /// </summary> /// <param name="context">当前上下文对象</param> /// <param name="responseAnnoto">特性参数</param> private static void InitializeCachePolicy(HttpContext context, ResponseAnnotationAttribute responseAnnoto) { if (responseAnnoto.CacheDuration > 0) { context.Response.Cache.SetCacheability(HttpCacheability.Server); context.Response.Cache.SetExpires(DateTime.Now.AddSeconds((double)responseAnnoto.CacheDuration)); context.Response.Cache.SetSlidingExpiration(false); context.Response.Cache.SetValidUntilExpires(true); if (responseAnnoto.ParameterCount > 0) { context.Response.Cache.VaryByParams["*"] = true; } else { context.Response.Cache.VaryByParams.IgnoreParams = true; } } else { context.Response.Cache.SetNoServerCaching(); context.Response.Cache.SetMaxAge(TimeSpan.Zero); } }