Although bundling is a neat feature of VS, sometimes I want to have a script or css to be available to a particular page. This way, I can make sure that name conflicts and/or overrides will be avoided.
Is it possible to bundle files so that only global and page specific files are available?
So for example, say I have a page called Cabinet.cshtml. And I also have Cabinet.js and Cabinet.css files. On the other hand I have another page called AdminPanle.cshtml with files admin.js and admin.css.
Now, I would like these two views to have access only to their corresponding files and also jQuery and jQuery ui. So jQuery must be global.
So what's the problem? By default in your BundleConfig.cs you have:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
So put this bundles in your head of your _Layout.cshtml:
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryui")
And create 4 other bundles:
//scripts
bundles.Add(new ScriptBundle("~/bundles/cabinet").Include(
"~/Scripts/Cabinet.js"));
bundles.Add(new ScriptBundle("~/bundles/admin").Include(
"~/Scripts/admin.js"));
//styles
bundles.Add(new StyleBundle("~/Content/cabinet").Include("~/Content/Cabinet.css"));
bundles.Add(new StyleBundle("~/Content/admin").Include("~/Content/admin.css"));
Now you can separate theese scripts and styles and add them only on page that you need.
Also I suppose it's good to define 2 sections in your _Layout.cshtml in head tag.
<head>
//other scripts and styles here
#RenderSection("scriptslib", required: false)
#RenderSection("csslib", required: false)
</head>
So now in your Views (Cabinet.cshtml and AdminPanle.cshtml) you can place your libs where they suppose to be like this:
#section scriptslib{
#Scripts.Render("~/bundles/cabinet")
}
Related
I have a minimal ASP.NET MVC 5 app that was generated via Scaffolding. When I look at the generated _Layout.cshtml file I see a <script> tag toward the bottom of the page that loads jQuery but nowhere in _Layout.cshtm do I see a reference to a jQuery Validation module in a <script> tag.
If I go into Chrome Developer Tools, under the "Sources" tab, I can see that indeed jQuery.validate and jquery.validate.unobtrisuve are loaded! But how can they be loaded if they were not referenced by a <script> tag?
If you look in App_Start BundleConfig.cs you should see reference to:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
which defines the jQuery validation script bundle. Certain scaffold pages (for example Login) have:
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
referenced at the bottom of the View.
This is picked up by #RenderSection("scripts", required: false) at the bottom of _Layout (which renders the script if it is defined).
In my BundleConfig.cs file I am trying to include all of my angularjs files in one step. I have:
bundles.Add(new ScriptBundle("~/bundles/angular").Include(
"~/Scripts/angular.js",
"~/Scripts/angular-*"));
bundles.Add(new ScriptBundle("~/bundles/App").IncludeDirectory(
"~/App", "*.js", true));
The first include works fine. It includes all of the angular files; however, the second include does not. When I go to sources in the browser it does not show up.
Any Suggestions?
Apparently the step that I missed was adding:
#Scripts.Render("~/bundles/app")
To the _Layout.cshtml page.
Now everything works as intended
I did some searching on this but couldn't find out why, but in my project, commenting out a line in the .Include() breaks the entire ScriptBundle. If I comment out one line, the other javascript files will not load.
Can you comment out individual bundles, or do you need to remove them and maybe put the comment below the code in case you need to put it back?
Ex.
This works:
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/bootstrap-multiselect.js",
"~/Scripts/bootstrap-datepicker.js",
"~/Scripts/respond.js"));
This causes the other javascript files in the ScriptBundle not to get loaded.
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
// "~/Scripts/bootstrap-multiselect.js",
"~/Scripts/bootstrap-datepicker.js",
"~/Scripts/respond.js"));
Commenting out a line like you show shouldn't be a problem. Check your rendered script sections -- If you see the bootstrap resources included on your page then the bundler is working.
If your other scripts stop working then this indicates a problem with your javascript still referencing the removed bundle resource.
of course you can comment some lines (as you did), but remember you need to recompile your project to get affected
Basically after a bunch of work I've finally managed to get up and running with Bootstrap in my ASP.NET MVC4 project.
Here is my folder structure:
Here is my bundleconfig:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/bootstrapjs").Include(
"~/Scripts/bootstrap.js"));
bundles.Add(new ScriptBundle("~/Content/bootstrapcss").Include(
"~/Content/bootstrap.css",
"~/Content/bootstrap-responsive.css"));
}
However when I try to add an HTML element with an icon:
<button type="submit" class="btn"><i class="icon-search"></i></button>
The icon does not appear.
The last bundle needs to be a StyleBundle, not a ScriptBundle. Here's an example from one of my projects:
bundles.Add(new StyleBundle("~/bundles/css").Include(
"~/content/css/bootstrap.min.css",
"~/content/css/font-awesome.min.css",
"~/content/css/common.css"));
I should note that I organize my CSS files into a specific folder, and this specific project uses FontAwesome in place of the Glyphicons.
As noted from the comments, the default Bootstrap package assumes that the CSS files are in one folder, and that the icon sprite file is in another folder at the same level as the CSS folder. Based on that assumption, the path to the sprite is set to "../img/glyphicons-halflings.png". If your files are not located in the same places, you need to manually edit the path, or use the Customizer to build a download that has the correct path for both the light and the dark versions of the sprite file.
I added the bundle in BundleConfig.cs as shown below:
bundles.Add(new ScriptBundle("~/bundles/angular").Include("~/Scripts/angular.min*"));
In the _Layout.cshtml I have the following:
<body>
#RenderBody()
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/angular")
#RenderSection("scripts", required: false)
</body>
</html>
In the network traffic it does not show the angular file.
Bundling in ASP.NET MVC is quite clever in that it understands *.min and when to use or no to use it. So if you do this in your bundle:
bundles.Add(new ScriptBundle("~/bundles/angular").Include("~/Scripts/angular.js"));
In debug-mode, it will send a request for "/scripts/angular.js" and in release-mode it will send a request to "/scripts/angular.min.js"
In order to benefit from this, you should put both the original and the minified file in your scripts folder. That way, when you're debugging you have full access to the uncompressed source and in your production environment, the optimized file will be loaded
you have given incorrect file name
~/Scripts/angular.min*
instead it should be
~/Scripts/angular.js
.min would automatically be added in the production mode\
Bundler not including .min files
in my case the problem was with Global.asax.cs
you need to have
BundleConfig.RegisterBundles(BundleTable.Bundles);
in your Application_Start()