简体   繁体   English

如何在asp.net中捆绑js文件?

[英]How to bundle js files in asp.net?

I have a angular application with lot of js files , how can I bundle all those files. 我有一个带有大量js文件的角度应用程序,我如何捆绑所有这些文件。

Every where I see in web , they suggest to use BundleConfig to bundle js files but I'm using empty web application template in asp.net . 我在网上看到的每个地方,他们都建议使用BundleConfig捆绑js文件,但我在asp.net中使用空的web应用程序模板。

How to bundle js files in an order so that no conflicts would appear and there would be no problem in picking absolute paths and relative paths 如何在一个顺序中捆绑js文件,以便不会出现冲突,并且在选择绝对路径和相对路径时没有问题

How about create a new project that is a full MVC project (select MVC as the tempalte not 'empty'. Then look under the App_Start/BundleConfig.cs, create that exact file and register it as the default project does in the global.asax.cs? 如何创建一个完整的MVC项目的新项目(选择MVC作为tempalte而不是'空'。然后在App_Start / BundleConfig.cs下查看,创建该文件并将其注册为global.asax中的默认项目。的.cs?

global.asax.cs: 的global.asax.cs:

BundleConfig.RegisterBundles(BundleTable.Bundles);

App_Start/BundleConfig.cs App_Start / BundleConfig.cs

using System.Web;
using System.Web.Optimization;

namespace WebApplication1
{
    public class BundleConfig
    {
        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }
}

To be honest though. 说实话。 I would use a Node based tool like webpack or gulp to do this. 我会使用基于节点的工具,如webpack或gulp来做到这一点。 You CAN use bundles but they are not as powerful and you miss out on a lot of things you could do in a front-end build. 您可以使用捆绑包,但它们不是那么强大,您可能会错过在前端构建中可以执行的许多操作。 It would help a lot more in terms of the correct load order for example. 例如,在正确的加载顺序方面,它会有很多帮助。

In common, we have 2 ways to bundle AnguarJS/ASP.NET: 通常,我们有两种方法来捆绑AnguarJS / ASP.NET:

  1. use bundle like above post BundleCollection from Justsayno 像Justsayno一样使用BundleCollection上面的bundle
  2. Use compress JS tool like grunt or gulp. 使用压缩JS工具,如grunt或gulp。 Sample. 样品。 https://github.com/MarlabsInc/webapi-angularjs-spa OR https://github.com/dchill72/NpmBowerGulpSample https://github.com/MarlabsInc/webapi-angularjs-spahttps://github.com/dchill72/NpmBowerGulpSample

Hope it help! 希望它有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM