public static HandlerMethods GetHandlerMethods(HttpContext context, string requestPath) { string path = VirtualPathUtility.ToAbsolute(requestPath); string cacheKey = "CooliteHandlerMethods_" + path; HandlerMethods handlerMethods = null; if (!IsDebugging) { handlerMethods = context.Cache[cacheKey] as HandlerMethods; } if (handlerMethods == null) { Type requestedType = BuildManager.GetCompiledType(path); if (requestedType == null) { requestedType = BuildManager.CreateInstanceFromVirtualPath(path, typeof(System.Web.UI.Page)).GetType(); } handlerMethods = new HandlerMethods(requestedType); if (!IsDebugging) { PutToCache(path, context, cacheKey, handlerMethods); } } return(handlerMethods); }
public static HandlerMethods GetHandlerMethodsByType(HttpContext context, Type type, string requestPath, bool ignoreCache) { string cacheKey = string.Concat("CooliteHandlerMethods_", requestPath, type.Namespace, type.Name); HandlerMethods handlerMethods = null; if (!IsDebugging && !ignoreCache) { handlerMethods = context.Cache[cacheKey] as HandlerMethods; } if (handlerMethods == null) { handlerMethods = new HandlerMethods(type); if (!IsDebugging && !ignoreCache) { if (string.IsNullOrEmpty(requestPath) || !Directory.Exists(requestPath)) { context.Cache.Insert(cacheKey, handlerMethods); } else { context.Cache.Insert(cacheKey, handlerMethods, new CacheDependency(context.Server.MapPath(requestPath))); } } } return(handlerMethods); }
private void ProcessRequest(HttpApplication app, HttpRequest request) { AjaxResponse responseObject = new AjaxResponse(true); try { HttpContext context = HttpContext.Current; HandlerMethods handler = HandlerMethods.GetHandlerMethods(context, request.FilePath); string methodName = HandlerMethods.GetMethodName(context); if (handler == null) { throw new Exception(string.Format("The Method '{0}' has not been defined.", request.FilePath)); } if (string.IsNullOrEmpty(methodName)) { return; throw new Exception("No methodName has been set in the configuration."); } AjaxMethod ajaxMethod = handler.GetStaticMethod(methodName); if (ajaxMethod == null) { throw new Exception(string.Format("The static AjaxMethod '{0}' has not been defined.", ajaxMethod)); } object result = ajaxMethod.Invoke(); if (!ScriptManager.AjaxSuccess) { responseObject.Success = false; responseObject.ErrorMessage = ScriptManager.AjaxErrorMessage; } else { responseObject.Result = result; } } catch (Exception e) { responseObject.Success = false; responseObject.ErrorMessage = IsDebugging ? e.ToString() : e.Message; } app.Context.Response.Clear(); app.Context.Response.ClearContent(); app.Context.Response.ClearHeaders(); app.Context.Response.StatusCode = 200; app.Context.Response.ContentType = "application/json"; app.Context.Response.Charset = "utf-8"; app.Context.Response.Cache.SetNoServerCaching(); app.Context.Response.Cache.SetMaxAge(TimeSpan.Zero); app.Context.Response.Write(responseObject.ToString()); app.CompleteRequest(); }
public void ProcessRequest(HttpContext context) { AjaxResponse responseObject = new AjaxResponse(true); try { HandlerMethods handler = HandlerMethods.GetHandlerMethods(context, context.Request.FilePath); string methodName = HandlerMethods.GetMethodName(context); if (handler == null) { throw new Exception(string.Format("The Method '{0}' has not been defined.", context.Request.FilePath)); } if (string.IsNullOrEmpty(methodName)) { throw new Exception("No methodName has been set in the configuration."); } AjaxMethod ajaxMethod = handler.GetStaticMethod(methodName); if (ajaxMethod == null) { throw new Exception(string.Format("The static AjaxMethod '{0}' has not been defined.", methodName)); } responseObject.Result = ajaxMethod.Invoke(); } catch (Exception e) { responseObject.Success = false; responseObject.ErrorMessage = IsDebugging ? e.ToString() : e.Message; } context.Response.Cache.SetNoServerCaching(); context.Response.Cache.SetMaxAge(TimeSpan.Zero); context.Response.Write(responseObject.ToString()); context.Response.End(); }
private static void PutToCache(string path, HttpContext context, string cacheKey, HandlerMethods handlerMethods) { BuildDependencySet dependencySet = BuildManager.GetCachedBuildDependencySet(context, path); if (dependencySet != null) { IEnumerable virtualPaths = dependencySet.VirtualPaths; if (virtualPaths != null) { List <string> paths = new List <string>(); foreach (string _path in virtualPaths) { paths.Add(context.Server.MapPath(_path)); } context.Cache.Insert(cacheKey, handlerMethods, new CacheDependency(paths.ToArray())); return; } } context.Cache.Insert(cacheKey, handlerMethods); }