Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc4 bundle, how it is working?

in mvc4 they use bundles to call all the scripts and css files once. as far as i know, the ordering of js and cs files is important when you call them. if i use bundles, how am i going to know if the css and js files are in the correct order inside the bundle? and can i customize the ordering?

i am having a problem with my datepicker now, it seems its css file/theme is not loading properly so i want to check how bundles order the css/js files... thanks :)

<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/themes/base/css")" rel="stylesheet" type="text/css" />
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>
like image 904
raberana Avatar asked Jun 01 '12 04:06

raberana


People also ask

How does bundling work in MVC?

Bundling is one of the features of MVC. By implementing this, we can improve performance request load time. Minification is the process of removing unnecessary data without changing its functionality such as removing white spaces, comments, converting the large variable names to small, etc.

How bundling and minification works in .NET core?

What is bundling and minification. Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.

What is difference between bundling and minification?

Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.


2 Answers

A late answer to that question, but ASP.NET MVC orders files by alphabetically. Also you can use IBundleOrderer interface to manually order your script files.

For example, use custom IBundleOrderer implementation like that :

Bundle myBundle = new Bundle("~/bundles/SiteScripts", new JsMinify());
myBundle.IncludeDirectory("~/Scripts/SiteScripts", "*.js");
myBundle.Orderer = new MyBundleOrderer();
bundles.Add(myBundle);

MyBundleOrderer takes high priority scripts from web.config file :

public class MyBundleOrderer : IBundleOrderer
{
    public IEnumerable<System.IO.FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
    {
        if (ConfigurationManager.AppSettings["HighPriorityScripts"] != null)
        {
            string[] highPriorityScripts = ConfigurationManager.AppSettings["HighPriorityScripts"].Split(',');
            List<FileInfo> listFiles = new List<FileInfo>(files);
            List<FileInfo> orderedFiles = new List<FileInfo>();

            // Add high priority files in order : 
            foreach (string highPriorityFile in highPriorityScripts)
            {
                FileInfo nextFileInfo = listFiles.Find(delegate(FileInfo arg) 
                                {
                                    return arg.Name == highPriorityFile;
                                }
                              );
                if (nextFileInfo != null)
                {
                    orderedFiles.Add(nextFileInfo);
                }
            }

            // Add remaining files to bundle : 
            foreach (FileInfo lowPriorityFile in listFiles)
            {
                if (!orderedFiles.Contains(lowPriorityFile))
                {
                    orderedFiles.Add(lowPriorityFile);
                }
            }

            return orderedFiles;
        }
        return files;
    }
}
like image 54
Canavar Avatar answered Sep 29 '22 18:09

Canavar


You can control the order of the "css" and "js" files by creating your own bundles as shown on this thread.

One important thing is you have to replace

BundleTable.Bundles.RegisterTemplateBundles();

with

BundleTable.Bundles.EnableDefaultBundles();

in Global.asax.cs

like image 29
VJAI Avatar answered Sep 29 '22 16:09

VJAI