Install-package microsoft.aspnet.web.optimization
After googling here and there I found a great code snippet here on StackOverflow. All credit goes to author Ranjeet patil thanks! Using this clean and awesome CSharp extension method - we get an elegant and effective code inside BundleConfig. How to use bundling and minification in ASP. Table of contents. What you will learn? Installing the Web. Install-Package Microsoft. Add styleBundle ; BundleTable.
Please note that you may have some script refreshing problems in the browser, luckily there is an elegant way to avoid this. MapPath file. ToString ; file. So let's have a look at this class and its methods because this will be used in all three implementations. This class has Include method which takes an array of string s, where each string is a virtual path to resource. We can add multiples files as follows:. But if you want to include all files of a particular directory, then this class provides one more method called IncludeDirectory.
You can use this method as follows:. Now, the next question is we have created bundles, now how will we include in view or aspx file?
We will use Styles. Render for CSS and Scripts. Render for script file as follows:. So here, I have covered all the basic details about this framework. Here I am going to describe from scratch by taking an empty template of Web form application:. Here, I have added 4 files in scripts folder and 2 files in Styles for demonstration.
You can add as per your requirement. You can see that there are many events in Global. You need to write the following code inside this event. Here, I am directly creating and adding two bundles into bundle table. You can define according to your classification and use. You can further classify a single bundle into other sub bundles.
So exactly what I did here is I bundled all JavaScript files into one bundle, it means they will load as a single entity not as multiple different files and same for CSS, I created a single bundle for all CSS files. I think now you can better understand practically how this framework optimizes the calls and loading of resources. In some articles, you might find that they have created a separate file called as BundleConfig for creating the bundle in a static file and they add bundles in global.
This provides one more level of abstraction. Since this is very basic article, I have directly created and added bundles in global. In the bin folder you can verify that the required DLLs have been added. Now your Solution Explorer should look like the following: Here I have added 2 files in the scripts folder and 2 files in Styles for demonstration.
You can add whatever you need to. Open the Global. You can see that there are many events in the Global. You need to write the following code inside this event. Now press F12 to see the real magic of the Web Optimization Framework. Click on the Script tab and select Test. You can see js? Here you can see that instead of separate js files only one bundle is loading. You can see the real call for the various resources in the Network tab. If you have not seen this developer tool of Internet Explorer and you did not observer here for normal application without ASP.
NET Optimization Framework then you might not be able to differentiate the real so for those people I can show you the proper difference. Now just return to your web. You can see here now that two separate JavaScript files are loading separately.
Assume you have added 20 js files to your view then there are 20 separate calls that will be made for 20 js files. Now just check in the network tab so here you can see there are 4 separate calls for 4 separate files.
0コメント