/// <summary> /// Adds to client script manager. /// </summary> /// <param name="page">The page.</param> /// <param name="link">The link.</param> private static void AddToClientScriptManager(Page page, HtmlLink link) { HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(new StringBuilder())); link.RenderControl(writer); page.ClientScript.RegisterClientScriptBlock(typeof(Page), writer.InnerWriter.ToString(), writer.InnerWriter.ToString()); }
/// <summary> /// Includes an html markup like <link rel=Stylesheet type=text/css href=[YOUR_HREF] /> /// </summary> /// <param name="htmlHelper"></param> /// <param name="path"></param> /// <param name="includeVersionBasedOnLastModifiedDate">adding the query param "v" = [yyyyMMddHHmmss] based on file last modified date</param> /// <returns></returns> public static IHtmlString IncludeCSS(this HtmlHelper htmlHelper, string path, bool includeVersionBasedOnLastModifiedDate = true) { string physicalPath = htmlHelper.ViewContext.HttpContext.Server.MapPath(path); var fileInfo = new FileInfo(physicalPath); if (!fileInfo.Exists) return new RawHtmlString(String.Empty); var lastModifiedDate = fileInfo.LastWriteTime; var sb = new StringBuilder(); using (var textWriter = new StringWriter(sb)) { using (var htmlWriter = new HtmlTextWriter(textWriter)) { var htmlLink = new HtmlLink(); htmlLink.Attributes["rel"] = "Stylesheet"; htmlLink.Href = String.Format("{0}?v={1:yyyyMMddHHmmss}", VirtualPathUtility.ToAbsolute(path), lastModifiedDate); htmlLink.Attributes["type"] = "text/css"; htmlLink.RenderControl(htmlWriter); } } return new RawHtmlString(sb.ToString()); }