Skip to main content

Using Mini Profiler's TimeScript With MVC4 Script And Style Bundles

I’m in the process of updating a couple of my projects to MVC4 and switching some of the CSS and JavaScript to use the new bundle and minification features. To simplify this while using script timing in Mini Profiler I created these two extension methods.

ProfilingExtensions.cs
public static class ProfilingExtensions
{
public static IHtmlString TimeScriptBundle(this WebPageBase page, string name, params string[] paths)
{
var bundle = Scripts.Render(paths);
return page.TimeScript(name, bundle);
}
public static IHtmlString TimeStyleBundle(this WebPageBase page, string name, params string[] paths)
{
var bundle = Styles.Render(paths);
return page.TimeScript(name, bundle);
}
}

Now instead of calling Time Script like this

Layout.cshtml
@this.TimeScript("Content: Main CSS", Styles.Render("~/Content/css"))
@this.TimeScript("Content: Base JS", Scripts.Render("~/bundles/jquery", "~/bundles/jqueryval"))

You can more easily do

Layout.cshtml
@this.TimeStyleBundle("Content: Main CSS", "~/Content/css")
@this.TimeScriptBundle("Content: Base JS", "~/bundles/jquery", "~/bundles/jqueryval")