Asp.Net MVC BundleConfig Comments - asp.net-mvc

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

Related

Is it possible to bundle scripts and styles per page

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")
}

Why is bundleConfig ignoring my include directory

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

MVC 5 Bundling Setting EnableOptimizations to True results in missing css

When I set EnableOptimizations to true, certain css attributes seem to be missing. This is similar to other questions, but for them the entire css files are missing. For my case, it only seems to be certain attributes.
Specifically, the padding on container-fluid from bootstrap.css goes missing. Everything else works fine though.
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/bootstrap-theme.css",
"~/Content/site.css"));
I do not have the folder ~Content/css to cause the virtual directory issue as most other answers have stated.

How to include Bootstrap icons in ASP.NET MVC4?

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.

MVC4 Bundling Wildcards - * vs. {version}

I understand and fully appreciate the new bundle wildcard {version} regex, as it will continue to pick up your script and style libraries even after updating, etc. The problem is that I cannot get it to work effectively.
I had previously use the following notation to pick up my jQuery scripts:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.*"));
and it would pick up all files matching the asterisk wildcard.
But when I tried the following:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
I noticed that the jQuery file was not written out to the _Layout.cshtml page.
NOTE - I am indeed using the following HTML code for both examples:
#Scripts.Render("~/bundles/jquery")
When I use the first example above (" * ") notation the following is written to the page:
<script src="/Scripts/jquery-1.7.2.js" type="text/javascript"></script>
However when I use the "{version}" notation, there is merely an empty space where the library should be written. This pertains to both jquery files as well as the associated CSS stylesheets.
It does not matter whether I am compiling for DEBUG or RELEASE mode. The results are the same. Obviously in release mode I'll get the optimized version when using the "*" notation:
<script src="/bundles/jquery?v=VUv2YOeuOgHad-FHZ-vGMDu9wN1j9VZGifI1SBUUSMs1" type="text/javascript"></script>
So being the perfectionist that all developers are, I would greatly like to see the {version} notation work for me, as I do keep up to date on new releases and would like this control on my deployments.
Any insight into what I may be doing wrong is greatly appreciated.
Thanks in advance,
Dan
Try updating Microsoft.AspNet.Web.Optimization and related NuGet packages: Microsoft.Web.Infrastructure, WebGrease, Antlr. Check not just "nuget.org" tab, but also "All".

Resources