/// <summary> /// Creates a composite resource from a list of URLs /// </summary> /// <param name="helper">the view helper for rendering tags, passed by default</param> /// <param name="scripts">list of scripts to compresss and combine</param> /// <param name="Name">Name of resource will only cache the first resource request with the specified name</param> /// <param name="type">type of resource css, javascript</param> /// <returns></returns> public static string CompositeScriptResource_CompositeScript( this HtmlHelper helper, List <string> scripts, string Name, ResourceType type, string clientID = "") { HttpContextBase context = helper.ViewContext.RequestContext.HttpContext; var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); if (CompositeResourceSettings.CompressionEnabled(context)) { BuildCompositeResource(scripts, Name, type, context); var url = urlHelper.Content("~/") + CompositeResourceSection.Instance.ResourceCompositionAction + Name + "/" + type.ToString() + (!string.IsNullOrEmpty(CompositeResourceSettings.Version) ? "?v=" + CompositeResourceSettings.Version : string.Empty); //return compressed script tag return(CreateTag(url, type, clientID)); } else { //return non compressed script tags return(BuildNonCompressedSript(scripts, urlHelper, type, clientID)); } }
/// <summary> /// Creates a script tag with link to pre-defined composite script resource /// </summary> /// <param name="helper">html helper</param> /// <param name="Name">Name of the composite script</param> /// <param name="clientID">ID of client script tag.</param> /// <returns> /// script tag for composite resource /// </returns> public static string CompositeScriptResource_DefinedCompositeScriptResource( this HtmlHelper helper, string Name, string clientID = "") { HttpContextBase context = helper.ViewContext.RequestContext.HttpContext; var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); CompositeScriptResourceDefinition csrdInstance = CompositeScriptResourceDefinition.GetInstance(context); Resource.CompositeResource resource = csrdInstance[Name]; if (CompositeResourceSettings.CompressionEnabled(context)) { BuildCompositeResource(resource.Scripts, resource.Name, resource.type, context); var url = urlHelper.Content("~/") + "CompositeScriptResource/getresource/" + Name + "/" + resource.type.ToString() + (!string.IsNullOrEmpty(CompositeResourceSettings.Version) ? "?v=" + CompositeResourceSettings.Version : string.Empty); //return compressed script tag return(CreateTag(url, resource.type, clientID)); } else { //return non compressed script tags return(BuildNonCompressedSript(resource.Scripts, urlHelper, resource.type, clientID)); } }
/// <summary> /// Handler returns JS script file compressed, non compressed or the cached composite/noncomposite file /// </summary> /// <param name="context">Current http context</param> public void ProcessRequest(HttpContext context) { try { context.Response.ContentType = (context.Request.CurrentExecutionFilePathExtension == ".css") ? "text/css" : "text/javascript"; //return the requested file if (!CompositeResourceSettings.CompressionEnabled(context)) { //return single file without looking in the cache context.Response.Write(GetNonCompressedResource(context.Request.PhysicalPath, context.Request.RawUrl, context.Response.ContentType, context));; } else { context.Response.Clear(); context.Response.Cache.SetCacheability(HttpCacheability.Private); if (CompositeResourceSettings.SetClientCacheExpire) { context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(CompositeResourceSection.Instance.ClientCacheDays)); } else { //don't expire cache context.Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1)); } string filename = VirtualPathUtility.GetFileName(context.Request.FilePath); string body = GetAndCompressSingleResource(context.Request.PhysicalPath, context.Request.RawUrl, context.Response.ContentType, context); context.Response.Write(body); } } catch (Exception ex) { Kesco.Lib.Log.Logger.WriteEx( new Kesco.Lib.Log.DetailedException("Compression Module Request Processing Exception", ex, context.Request.RawUrl) ); context.Response.ContentType = "text/plain"; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Write(ex.Message); } }