How can I use the script defer attribute for ASP MVC 4 Bundles with Scripts.Render

后端 未结 4 1900
太阳男子
太阳男子 2021-02-01 04:18

I have looked through Google and Stackoverflow and haven\'t found an answer for this. Is there any built in way to make a bundle execute as deffered or does someone know of an e

4条回答
  •  孤独总比滥情好
    2021-02-01 04:34

    The answer above is great. I just want to quickly paste my code over here for those who wants to have a more concise syntax.

    Add a new C# class

    // --------------------------------------------------------------------------------------------------------------------
    // 
    //   http://believeblog.azurewebsites.net/
    // 
    // --------------------------------------------------------------------------------------------------------------------
    
    using System.Web;
    using System.Web.Optimization;
    
    namespace MVCExtension
    {
        /// 
        ///     The scripts.
        /// 
        public static class Scripts
        {
            /// 
            /// Render scripts as deferred
            /// 
            /// 
            /// The paths.
            /// 
            /// 
            /// The .
            /// 
            public static IHtmlString RenderDefer(params string[] paths)
            {
                return Scripts.RenderFormat(@"", paths);
            }
        }
    }
    

    Then, use Razor syntax:

    @Scripts.RenderDefer("~/bundles/jquery")
    

    Or Webform syntax:

    <%: Scripts.RenderDefer("~/bundles/jquery") %>
    

提交回复
热议问题