I have a set of extension methods that I use (see below). You can use them as a base / example to create your own debug / release extension methods.
General debugging / release:
public static MvcHtmlString DebugReleaseString(this System.Web.Mvc.HtmlHelper html, string debugString, string releaseString) { string toReturn = debugString; #if !DEBUG if (!string.IsNullOrEmpty(releaseString)) toReturn = releaseString; #endif return MvcHtmlString.Create(toReturn); }
General debug / release usage:
@Html.DebugReleaseString("/images/myimage.jpg", "http://mycdn.com/images/myimage.jpg")
CSS debug / release tags:
public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName) { return html.CssTag(fileName, string.Empty); } public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName, string releaseFileName) { if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName"); string cssTag = string.Format( "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", html.MeDebugReleaseString(fileName, releaseFileName)); return MvcHtmlString.Create(cssTag); }
Using CSS tags for debugging / release:
@Html.CssTag("/styles/mystyle.css") @Html.CssTag("/styles/mystyle.css", "http://mycdn.com/styles/mystyle.css")
JS Debug / Release Tags:
public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName) { return html.JavascriptTag(fileName, string.Empty); } public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName, string releaseFileName) { if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName"); string jsTag = string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", html.MeDebugReleaseString(fileName, releaseFileName)); return MvcHtmlString.Create(jsTag); }
Using Jug tags for debugging / release:
@Html.JavascriptTag("/scripts/myscript.css") @Html.JavascriptTag("/scripts/myscript.css", "http://mycdn.com/scripts/myscript.js")
Additional debugging / release options:
public enum RenderModeEnum { Debug, Release, DebugAndRelease } public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName, RenderModeEnum tagRenderMode) { if (tagRenderMode == RenderModeEnum.DebugAndRelease) return html.CssTag(fileName); #if DEBUG if (tagRenderMode == RenderModeEnum.Debug) return html.CssTag(fileName); #else if (tagRenderMode == RenderModeEnum.Release) return html.CssTag(fileName); #endif return MvcHtmlString.Empty; } public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName, RenderModeEnum tagRenderMode) { if (tagRenderMode == RenderModeEnum.DebugAndRelease) return html.JavascriptTag(fileName); #if DEBUG if (tagRenderMode == RenderModeEnum.Debug) return html.JavascriptTag(fileName); #else if (tagRenderMode == RenderModeEnum.Release) return html.JavascriptTag(fileName); #endif return MvcHtmlString.Empty; }
Additional features for using debugging / release:
@Html.CssTag("/styles/mystyle.css", RenderModeEnum.DebugAndRelease) @Html.CssTag("/styles/mystyle.css", RenderModeEnum.Debug) @Html.CssTag("http://mycdn.com/styles/mystyle.css", RenderModeEnum.Release)