Bundling and minification without ASP.NET MVC

前端 未结 6 1211
遥遥无期
遥遥无期 2020-12-13 14:17

Is it possible to use bundling and minification from Microsoft.AspNet.Web.Optimization without having an MVC project?

I\'m creating an AngularJS site communicating w

6条回答
  •  不知归路
    2020-12-13 15:04

    In addition to the answers given above, I'd like to mention that there was one important step forgotten:

    After you've installed the NuGet package for Microsoft.AspNet.Web.Optimization and registered the bundles in the Global.asax (as explained in Claies answer), you need to add the render methods for styles and scripts as follows:

    <%= Styles.Render("~/bundles/css") %>
    <%= Scripts.Render("~/bundles/MattsUIBundle/js") %>
    

    This needs to be added to the ASPX page's head section in order to render the bundles "~/bundles/js" and "~/bundles/css" added earlier to your page. Without that it will not appear (see why do I need render?). It requires that you add

    <%@ Import Namespace="System.Web.Optimization" %>
    

    to line 2 of your page in order to register the namespace, assuming you have already added the NUGET package Microsoft.AspNet.Web.Optimization to your code.

    If you want to include more related files, do it like

    void Application_Start()
    {
        BundleCollection bundles=BundleTable.Bundles;
        var jsMattsUIBundle = new ScriptBundle("~/bundles/MattsUIBundle/js");
        jsMattsBundle.Include("~/Scripts/lib/jquery.min.js");
        jsMattsBundle.Include("~/Scripts/lib/jquery-ui.custom.min.js");
        jsMattsBundle.Include("~/Scripts/lib/jquery.watermark.min.js");
        bundles.Add(jsMattsBundle);
    }
    

    or more simply

        jsMattsBundle.Include("~/Scripts/lib/jquery.min.js",
                "~/Scripts/lib/jquery-ui.custom.min.js",
                "~/Scripts/lib/jquery.watermark.min.js");
    

    That will bundle together all the 3 scripts under the virtual path "~/bundles/MattsUIBundle/js".


    Important: Bundling will check if you are in debug mode or release mode. Optimizations only apply if you remove the debug flag in web.config

    
    

    or if you explicitly define inside of Application_Start you want to optimize regardless of being in debug mode:

    BundleTable.EnableOptimizations = true;
    

    Likewise, if you're using CDN support, turn it on via

    BundleTable.Bundles.UseCdn = true;   //enable CDN support
    

    which will allow you to declare

    var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";
    BundleTable.Bundles.Add(new ScriptBundle("~/bundles/jquery",
                jqueryCdnPath).Include("~/Scripts/jquery-{version}.js"));
    

    Note that the CDN will only be used in release mode. The following script ensures that a local copy of jQuery is loaded if the CDN loading fails:

        <%= Scripts.Render("~/bundles/jquery") %>
         
    

    assuming that you're providing a local copy of jQuery 1.7.1 at "~/Scripts".


    Note: In MVC Razor syntax rendering is done in the view as follows:

    @Scripts.Render("~/bundles/MattsUIBundle/js") 
    @Styles.Render("~/bundles/css") 
    

    More information can be found here.

提交回复
热议问题