Scripts.RenderFormat() and Styles.RenderFormat() methods of ASP.NET web optimization framework

Scripts.RenderFormat() and Styles.RenderFormat() methods of ASP.NET web optimization framework

The standard way to render a bundled script or css file is to use Render method of Scripts and Styles helper classes as follows in ASP.net MVC:

For script

@Scripts.Render("<Path to script bundle>")

For CSS

@Styles.Render("<Path to style bundle>")

The above code snippet would give you HTML output like

<script src="<Path to script bundle>"></script>

and

<link href="<Path to style bundle>" rel="stylesheet"/>

But there are limitations with the above approach.
1) You cannot add any custom attribute. e.g., To be W3C valid you have to add type=”text/javascript” with script element and type=”text/css” to link.
2) You cannot use async script attribute introduced in HTML5.

Here is the good news:

ASP.NET Web optimization framework introduced “RenderFormat” method in version 1.1.0. You can use this method to specify custom format while rendering a bundled script or CSS file.

Example,
Add [type=”text/javascript”]

@Scripts.RenderFormat("<script type="text/javascript" src="{0}"></script>","<Path to script bundle>")

Add [async]

@Scripts.RenderFormat("<script type="text/javascript" src="{0}" async></script>","<Path to script bundle>")

Add [type=”text/css”]

@Styles.RenderFormat("<link href="{0}" type="text/css" rel="stylesheet"/>","<Path to style bundle>")

Hope this piece of information helps you better utilize bundling and minification feature of ASP.net.