/// <summary> /// Creates the proper CSS link reference within the target CSHTML page's head section /// </summary> /// <param name="virtualPath">The relative path of the image to be displayed, or its directory</param> /// <returns>Link tag if the file is found.</returns> public static IHtmlString ImportStylesheet(string virtualPath) { ImageOptimizations.EnsureInitialized(); if (Path.HasExtension(virtualPath)) { virtualPath = Path.GetDirectoryName(virtualPath); } HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); string cssFileName = ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName; virtualPath = Path.Combine(virtualPath, cssFileName); string physicalPath = HttpContext.Current.Server.MapPath(virtualPath); if (File.Exists(physicalPath)) { TagBuilder htmlTag = new TagBuilder("link"); htmlTag.MergeAttribute("href", ResolveUrl(virtualPath)); htmlTag.MergeAttribute("rel", "stylesheet"); htmlTag.MergeAttribute("type", "text/css"); htmlTag.MergeAttribute("media", "all"); return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing))); } return(null); }
/// <summary> /// Prepares some field variables and check them if they are correct for sprite rendering. /// </summary> /// <returns>true if ready for sprite rendering</returns> private bool PrepareSpriteRendering() { ImageOptimizations.EnsureInitialized(); if (!EnableSprites) { return(false); } _cssFileName = ImageOptimizations.LinkCompatibleCssFile(new HttpContextWrapper(Context).Request.Browser); if (String.IsNullOrEmpty(_cssFileName)) { return(false); } string spriteDirectoryPath = Path.GetDirectoryName(ImageUrl); // Check that CSS file is accessible if (!File.Exists(Path.Combine(Context.Server.MapPath(spriteDirectoryPath), _cssFileName))) { return(false); } return(true); }
/// <summary> /// Creates a reference to the sprite / inlined version of the desired image including special attributes. /// </summary> /// <param name="virtualPath">The relative path of the image to be displayed</param> /// <param name="htmlAttributes">Html Attributes of IDictionary form</param> /// <returns>Image tag.</returns> public static IHtmlString Image(string virtualPath, IDictionary <string, object> htmlAttributes) { ImageOptimizations.EnsureInitialized(); TagBuilder htmlTag = new TagBuilder("img"); htmlTag.MergeAttributes(htmlAttributes); HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); if (ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) == null) { htmlTag.MergeAttribute("src", ResolveUrl(virtualPath)); return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing))); } else { htmlTag.AddCssClass(ImageOptimizations.MakeCssClassName(virtualPath)); htmlTag.MergeAttribute("src", ResolveUrl(ImageOptimizations.GetBlankImageSource(httpContext.Request.Browser))); return(new HtmlString(htmlTag.ToString(TagRenderMode.SelfClosing))); } }
protected override void OnPreRender(EventArgs e) { ImageOptimizations.EnsureInitialized(); if (Path.HasExtension(ImageUrl) || ImageUrl.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase) || ImageUrl.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase)) { ImageUrl = Path.GetDirectoryName(ImageUrl); } string cssFileName = ImageOptimizations.LinkCompatibleCssFile(new HttpContextWrapper(Context).Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName; // Set up fileName and path variables string localPath = Context.Server.MapPath(ImageUrl); // Check that CSS file is accessible if (!File.Exists(Path.Combine(localPath, cssFileName))) { return; } AddCssToPage(Page, Path.Combine(ImageUrl, cssFileName)); }