• Using SquishIt with Razor – ASP.Net MVC3 and MVC4

    by  • November 5, 2011 • 4 Comments

    I’ll assume you already know the reasons why you should minify and combine your CSS and Javascript.

    My favourite of the tools available for .Net is SquishIt by Justin Etheredge – here’s how I hook it up in my ASP.Net MVC4 (and MVC3) Razor projects..

    1. Get the bits, either directly or find it in NuGet
    2. Add a Project Reference to the SquishIt.Framework.dll
    3. Add SquishIt to the namespaces section in the Views/Web.config:

    1. Replace any <link /> and <script /> references with the following*:

    *I’m loading the <script />‘s in the <head /> here for brevity, but there’s nothing stopping us moving these two JavaScript statements (lines 15 & 22) to the bottom of our page so they’re just above the closing </body>, as is generally recommended.

    1. Finally, as you’ll have noticed in lines 11 & 20, we’re generating the squished files to a /Cache directory, so we need to create it and grant the IIS AppPool our site is running under create and modify permissions on it

     

    As I’m usually working with Layout pages, I’m sure you’ll have also noticed the @RenderSection()‘s in the example (lines 13 & 22) – that let’s me inject page-specific Squished CSS and/or Javascript as needed like so:

     

    Thank you JustinĀ  :)

    About Andrew Freemantle

    4 Responses to Using SquishIt with Razor – ASP.Net MVC3 and MVC4

    1. Avi
      December 22, 2011 at 2:55 pm

      Hi Andrew , Any idea y i keep getting this error when i run my application in release mode.

      System.IO.IOException: Unable to open file

      Thanks

    2. January 3, 2012 at 11:03 pm

      Hi Avi, thanks for leaving a comment.

      Without seeing your code, I would guess that you’re getting a permissions error when SquishIt is trying to generate the output file – might be worth checking over ‘step 5′..

      If you can let me know which version of Windows and IIS you are running I can give you a little more help.

    3. Avi
      January 11, 2012 at 7:45 pm

      @Andrew, yes, this was because of folder permission issue. couple of questions though

      1. whats the different b/w Squishit and SquishIt.Contrib.MVC
      2. How can i force squish to only work for release mode

    4. January 29, 2012 at 1:34 pm

      Hi Avi,

      1. whats the different b/w Squishit and SquishIt.Contrib.MVC?
      - Honestly, I don’t know. If you find out please let me know!

      2. How can i force squish to only work for release mode
      - SquishIt should only work in release mode.. that is it’s default behaviour. Why do you ask? What is happening in debug mode that makes you suspect SquishIt?

    Leave a Reply

    Your email address will not be published.

    Your name *

    Your website

    *