/// <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); string localSpriteDirectory = Path.GetDirectoryName(httpContext.Server.MapPath(virtualPath)); if (ImageOptimizations.LinkCompatibleCssFileName(httpContext.Request.Browser, localSpriteDirectory) == 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))); } }
/// <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); // Set up fileName and path variables string localPath = httpContext.Server.MapPath(virtualPath); string cssFileName = ImageOptimizations.LinkCompatibleCssFileName(httpContext.Request.Browser, localPath); if (cssFileName == null) { return(null); } 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); }