简体   繁体   中英

Add js files with hashed names (generated by Angular CLI) in a MVC view

I have an angular app, build with angular cli integrated in a MVC project.

I'm using ng build --prod --output-hashing none . The output files are copied in a folder in the MVC project. Since the file names are always the same, I'm just referencing them in a cshtml to load the angular app.

...
<script type="text/javascript" src=".../inline.bundle.js"></script>
<script type="text/javascript" src=".../vendor.bundle.js"></script>
<script type="text/javascript" src=".../script.bundle.js"></script>
...

But if I use ng build --prod , this approach doesn't work, because the generate files have hashes in their names: inline.318b50c57b4eba3d437b.bundle.js

How can I include those files in the view

Keep using --output-hashing none and create a ScriptBundle in AppStart/BundleConfig.cs which will add the same functionality with a hash in the bundle-name.

BundleConfig.cs:

            bundles.Add(new ScriptBundle("~/bundles/app").Include(
                "~/Scripts/app/runtime.js",
                "~/Scripts/app/polyfills.js",
                "~/Scripts/app/scripts.js",
                "~/Scripts/app/vendor.js",
                "~/Scripts/app/main.js"

                //you might want the styles.js here as well for DEBUG-only - I left them out of this example
            ));
#if DEBUG
#else
            BundleTable.EnableOptimizations = true;
#endif

Index.cshtml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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