Sunday, January 20, 2013

50 Ways to Avoid, Find and Fix ASP.NET Performance Issues

I contributed to a free book that got launched last week. It's a book about quick tips and tricks for performance:

50 Ways to Avoid, Find and Fix ASP.NET Performance Issues

My tip:
"Projects that use multiple levels of cache often demonstrate a misunderstanding of why caching is required in the first place. Caching is not synonymous with performance. Your code should already be efficient. Caching should only be used as a last resort, after you’ve made all possible (and sensible) code optimizations."
It sounds obvious doesn't it. But how many projects have you come across where caching was hiding inherent performance problems?

Anyway, the book is part of a marketing campaign for Red Gate Performance Profiler. So even though it is free, you may be contacted by Red Gate via email. There's no such thing as a free lunch now is there.

In any case, if you're a .net programmer then you should definitely have a read. It gives you 50 quick bits of useful knowledge. Most of which, you should definitely be aware of if you're a .net programmer.

Get it here:

Friday, January 18, 2013

Phishing - You're doing it wrong!

I found this email in my spam box yesterday:

This email is from the fbi. We have an information for you regarding the person you are transacting with online. So we advise you to pause any transaction now and get back to us through this email below for further information.
Contact *******  ****** of the fbi secrete service on **********

There are so many things wrong with this email. But the first thing that stuck out to me was the Hotmail address. Seriously! Why would the FBI use a Hotmail address? 

Phishing... You're doing it wrong!

Note: I blocked details because it looked like a hacked personal account.

Thursday, January 3, 2013

Dead-Simple Minification and Combination of CSS and JS files

In this post I'm going to give a quick example of how to compress and combine CSS and JS files in a build step, and to link these up in your .net application using razor. It's so damn easy that there's no reason why your next project shouldnt use it. I'm going to assume that you already know about MSBuild.

A special note: Since we're dealing with combining files, it is worth noting that your CSS needs to be done right (of course!). If you have overriding styles for certain pages, you'll need to make sure that your CSS targeting is more specific otherwise your pages might look strange.

Lets begin....

First, get YUI:

I put YUI under a root tools folder: \Tools\YUI\


The following file will compress all files under \css and \scripts folders, into single files: minified.css and minified.js (respectively). See the properties MinifyCssOutput and MinifyJSOutput if you want to change these file names. You can exclude files using the properties MinifyJsExclusions and MinifyCssExclusions.

The following code should be self explanatory:

 <?xml version="1.0" encoding="utf-8"?>  
 <Project DefaultTargets="RunAll" xmlns="">  
   <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>  
   <UsingTask TaskName="CssCompressorTask" AssemblyFile="..\..\Tools\YUI\Yahoo.Yui.Compressor.Build.MsBuild.dll" />  
   <UsingTask TaskName="JavaScriptCompressorTask" AssemblyFile="..\..\Tools\YUI\Yahoo.Yui.Compressor.Build.MsBuild.dll" />  
   <!-- Settings -->  
     <!-- Define output files -->  
     <!-- Define files to exclude-->  
   <Target Name="RunAll">  
     <Message Text="RunAll"/>  
     <!-- Ensure output files do not already exist -->  
     <Delete Files="$(MinifyCssOutput)" />  
     <Delete Files="$(MinifyJsOutput)" />  
     <CallTarget Targets="Minify"/>  
   <Target Name="Minify">  
     <Message text="Compressing JavaScript and CSS files"/>  
     <!-- Define files to include -->  
     <CreateItem Include="$(TargetPath)Scripts\**\*.js" Exclude="$(MinifyJsExclusions)" >  
       <Output TaskParameter="Include" ItemName="JsFiles"/>  
     <CreateItem Include="$(TargetPath)Css\**\*.css" Exclude="$(MinifyCssExclusions)">  
       <Output TaskParameter="Include" ItemName="CssFiles"/>  
     <!-- Do compression -->  

The file can be called from your main MSBuild file using:
 <MSBuild Projects="$(MSBuildStartupDirectory)Build\Minify.msbuild"/>  

Note: My MSBuild transform files are in a root build folder: \Build\Transforms\, and my YUI files are under \Tools\YUI\, so you'll notice that my AssemblyFile attributes start with ..\..\Tools\YUI. Change this and any other paths to fit your folder structure.

Modifying your layouts

All you need to do is add the following razor code to your layouts...

Put this in your <head>
 @if (HttpContext.Current.IsDebuggingEnabled)  
      @*put your css files here*@  
      <link href="/css/layout.css" rel="stylesheet" type="text/css" />  
      <link href="/css/someother.css" rel="stylesheet" type="text/css" />  
      <link href="/css/minified.css" rel="stylesheet" type="text/css" />  

Put this in where your javascript files are. I like to put it at the end of <body>. Note: I put jQuery in the <head>. I think this is useful but some may disagree.

 @if (HttpContext.Current.IsDebuggingEnabled)  
      @*put your js files here*@  
      <script src="/scripts/site.main.js" type="text/javascript"></script>  
      <script src="/scripts/someother.js" type="text/javascript"></script>  
      <script src="/scripts/minified.js" type="text/javascript"></script>  

You probably already noticed that when your application is in debug mode, then all the files will be rendered, otherwise only the minified file is rendered.

If you have nested layouts, that's fine. You can have the "if" statement at all levels, just don't include the "else" part in the child layouts.

Minification and combination of CSS and JS files is dead simple! Do it!