Last active
December 29, 2016 05:54
-
-
Save deanhume/8595343 to your computer and use it in GitHub Desktop.
An ASP.NET MVC bundle extension file to handle optional HTML attributes such as async, defer, and media for CSS links.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Optimization.Custom | |
{ | |
using System.Collections.Generic; | |
using System.Web; | |
using System.Text; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
/// <summary> | |
/// An extension class for the System.Web.Optimization bundling tools. | |
/// </summary> | |
public static class Bundler | |
{ | |
/// <summary> | |
/// Renders the styles tag with optional html attributes. | |
/// </summary> | |
/// <param name="path"> | |
/// The virtual path of the styles. | |
/// </param> | |
/// <param name="htmlAttributes"> | |
/// The html attributes. | |
/// </param> | |
/// <returns> | |
/// The <see cref="IHtmlString"/>. | |
/// </returns> | |
public static IHtmlString RenderStyles(string path, object htmlAttributes) | |
{ | |
var attributes = BuildHtmlStringFrom(htmlAttributes); | |
string completedTag = string.Empty; | |
#if DEBUG | |
var originalHtml = Styles.Render(path).ToHtmlString(); | |
completedTag = originalHtml.Replace("/>", attributes + "/>"); | |
#else | |
completedTag = string.Format( | |
"<link rel=\"stylesheet\" href=\"{0}\" type=\"text/css\"{1} />", | |
Styles.Url(path), attributes); | |
#endif | |
return MvcHtmlString.Create(completedTag); | |
} | |
/// <summary> | |
/// Renders the scripts tag with optional html attributes. | |
/// </summary> | |
/// <param name="path">The virtual path of the scripts.</param> | |
/// <param name="htmlAttributes">The html attributes.</param> | |
/// <returns>The <see cref="IHtmlString"/>.</returns> | |
public static IHtmlString RenderScripts(string path, object htmlAttributes) | |
{ | |
var attributes = BuildHtmlStringFrom(htmlAttributes); | |
string completedTag = string.Empty; | |
#if DEBUG | |
var originalHtml = Scripts.Render(path).ToHtmlString(); | |
completedTag = originalHtml.Replace("/>", attributes + "/>"); | |
#else | |
completedTag = string.Format( | |
"<script src=\"{0}\" {1} />", | |
Scripts.Url(path), attributes); | |
#endif | |
return MvcHtmlString.Create(completedTag); | |
} | |
/// <summary> | |
/// Use the html attributes and loop through in order | |
/// to add to the completed tag. | |
/// </summary> | |
/// <param name="htmlAttributes">The html attributes.</param> | |
/// <returns>An HTML string containing the html attributes</returns> | |
private static string BuildHtmlStringFrom(object htmlAttributes) | |
{ | |
// Try and safely cast | |
var routeHtmlAttributes = htmlAttributes as IDictionary<string, object> ?? new RouteValueDictionary(htmlAttributes); | |
var attributeBuilder = new StringBuilder(); | |
foreach (var attribute in routeHtmlAttributes) | |
{ | |
attributeBuilder.AppendFormat(" {0}=\"{1}\"", attribute.Key, attribute.Value); | |
} | |
return attributeBuilder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I used this code in my project and I have a problem in release mode (Not in Debug).
The script tag is not correct and caused to commenting all codes after self in browser!
I find this issue in line 63:
and I fixed it by adding script end tag!
and now fixed code is:
Thanks for all codes :)