Search code examples
c#asp.netasp.net-mvcrazor

How to preload bundle in asp.net mvc 4.5


I am using following code, to add bundle to my layout.

@Scripts.Render("~/bundles/ngapp")

I would also like to preload our Bundle, but I do not know exact name and query string version which will be rendered after deploy. How can I change this piece of code to be something like this:

  <link rel="preload" href="@Scripts.BundleName("~/bundles/ngapp")" as="script">
@Scripts.Render("~/bundles/ngapp")

Solution

  • I think there are multiple ways to do this.

    You can make use of the 'ResolveBundleUrl':

    <link rel="preload" href="@(BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts"))" as="script">
    

    Or you can use the 'Scripts.RenderFormat':

    @Scripts.RenderFormat("<link rel='preload' href='{0}' as='script'>", "~/bundles/scripts")