Friday, August 17, 2012

MVC 4 Bundles - Where are my css referenced images?


Synopsis:

If StyleBundle or ScriptBundle virtual path is not configured to a valid (virtual) path, css/js files with relative reference links will be broken.

Remedy:

Tip 1:
In BundleConfig.cs and in invoking .Render methods in .cshtml, use the actual (virtual) path:
  • bundles.Add(new StyleBundle("~/Content/themes/main/css").Include("~/Content/themes/main/style.css"));
  • @Styles.Render("~/Content/themes/main/css ")
Tip 2:
Avoid bundling files that aren't located in the same directory

Other things that helped me:
- Be sure to activate Bundle functionality in BundleConfig.cs or Web.config:
  • BundleConfig.cs:  BundleTable.EnableOptimizations = true;
  • Web.config:  
- It is good to bundle even single files;  they'ill at least be minified
- Turn off bundling until deploying to production (it is easier to debug js)

Details:

I'm building my first production app in MVC 4 and exploring the new Bundles functionality.  Bundles is a great feature for optimizing applications for fast downloading.  Once it is configured, it minifies JavaScript and CSS file by doing things such as removing whitespace and even shortening variable names to make it as compact as possible.  It will even bundle (thus the name) css and js files into single files for even greater efficiencies.

When I first bundled my files together, I found I was missing images that were linked relative to my stylesheets.  After some digging I found it isn't a good idea to use a fake virtual path when declaring a StyleBundle for the bundles collection.  Fake virtual paths are ok, as long as the css or js file does not reference a relative file.


Tuesday, April 10, 2012

NuGet not installing NUnit 2.6x tools in Visual Studio .net 2010

I've been working on learning ASP .NET MVC 3 and started a video tutorial from PluralSight - very informative and goes step-by-step to build on learning.

I issue I came across was using the the NuGet utility to add the NUnit package and references to do unit testing. The current version is 2.6x, and the instruction from PluralSight was provided with 2.5x; I found the current version a bit harder to set up than the demo showed. When I used NuGet to get the needed packages for the test project, although it added the needed references, it didn't install the nunit-console (at least I couldn't find it) in the solution directory [$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\nunit-console].

The following has seemed to remedy the issue:
  • Go to the NUnit site and downloaded the .msi.
  • Once installed (by double-clicking the downloaded msi), copy needed files in the install directory [C:\Program Files (x86)\NUnit 2.6\bin]
  • Paste in [$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\]
That did it - the post-build event command line code of the test class library executed without issue. For reference, here is the command line:
$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\nunit-console $(TargetPath)