Ben McCallum's Blog

Dissecting ASP.NET MVC3, CSS3, HTML5, jQuery and a whole lot of other things ending with numbers…

Mads Kirstensen’s Free Image Optimiser

I found this a while ago but never really tried using it until just now: http://visualstudiogallery.msdn.microsoft.com/a56eddd3-d79b-48ac-8c8f-2db06ade77c3/

This is a plugin by Mads Kristensen a Program Manager at Microsoft who does a lot of work with Visual Studio tooling. He wrote this tool which lets you right click a folder of images or a single image in the Visual Studio Solution Explorer and select “Optimise images”.

using Image Optimiser

It then performs some magic – calling image optimisation web services to optimise the images by removing unneeded metadata in the image file. It can also generate base64 strings for embedding images in CSS stylesheets.

On average I see around a 30% saving in file size and the optimisation is lossless meaning the image quality remains the same. It’s a quick and easy thing to do just before a new deployment to make browsing our websites much faster.

Just be sure to check out the images you want to be optimised otherwise the plugin will just skip them.

Hope you find it useful.

WCF Web Service Wrapper: Closing, Disposing and Aborting best practices

When calling methods on a WCF web service it’s best practise to do the following:
(To avoid the using keyword with WCF clients: http://msdn.microsoft.com/en-us/library/aa355056.aspx)

But all this garbage is exhausting and repetitive when you are calling the web service in many places.

One solution is to inherit IDisposable and implement Dispose() for the web service but you’ll need to do that for every web service you consume meaning more duplicate code and using partial classes in combination with the auto-generated code from your service reference in Visual Studio.

Instead, I’ve come up with a hybrid of many ideas on the web that essentially wraps the WCF web service, performs any method calls and closes/disposes/aborts the connection gracefully:

WCF Service Wrapper class

 

An example of usage:

It’s a very elegant, and highly re-usable piece of code that I’ll be using in all apps that consume WCF web services. Using generics the same Service class can make calls to different services meaning you can plug-n-play this code in all your client apps and never write a try catch finally block around WCF service calls again. And if you ever need to customise the way service calls are handled, errors logged, etc. it’s all in one spot! Enjoy!

How to have multiple MVC websites under a single Go Daddy hosting account

The other day I decided to expand my repertoire of domain names and make some new websites. Namely, benmccallum.net and Halo Game Finder. My plan was to host them on my existing hosting plan with GoDaddy and after reading some documentation  it seemed this would be possible on my single “Deluxe Windows Hosting” plan. But for some reason I couldn’t get my new domains to serve any content, not even a simple index.html file with some test text in it.

I’ll walk you through the steps and then show you where I had issues.

  1. Buy the domain names with GoDaddy. 
  2. Add them to your hosting account.
    Go to your hosting control center and under the Domains tab add your new domains.
    When you add a new domain you simply point the URL to a particular folder within your hosting filesystem. Note: If that folder doesn’t exist already, one will be made for you.

    Since I already had a primary domain name I currently had something like this:
    http://reach4urphone.com —> /
    In other words, my primary domain was pointing to the root of my hosting filesystem.

    Any additional domains are then simply subfolders within the root. After adding my new domains I had:
    http://reach4urphone.com —> /
    http://halogamefinder.com —> /halogamefinder.com   (a directory sitting under root)
    http://benmccallum.net —> /benmccallum.net                (another directory sitting under root)
    Feel free to name these folder however you like but it’s convention/best practise from my experience to name them according to their domain name.

  3. Upload the content of your new websites into their associated subfolders
  4. Test your websites…
    And this is where I had issues. For some reason none of the new websites would work. Not even if I put a simple test index.html in there and nothing else. Turns out the problem was in the primary website’s web.config under the root. This website was an MVC3 app and it’s web.config seemed to be determining the state of all the other websites sitting in their own folders/virtual directories. Because MVC3 apps at the moment require bin deployment (where the MVC Assemblies/DLLs are deployed into the bin folder of the app) I think the child websites were getting confused trying to find those DLLs. To solve the problem I wrapped the system.web element of the root website’s web.config with a location element. See below:
MVC3 app web.config fix

MVC3 app web.config fix

Hope this helps others facing the same problem.

Making older browsers recognize and correctly style the new HTML5 semantic tags

I recently converted a web app I was working on (Reach4urPhone) to use HTML5 semantic markup. Instead of using divs with id attributes such as “header”, “nav” and “footer”, I replaced them with the semantic header, nav and footer HTML5 elements. Makes sense right? All I had to do was update the HTML and my CSS selectors and all the browsers on my computer were rendering my web pages correctly. It was a five minute job and life was good… or so I thought.

A week later I had a job interview at a web development company. Whilst proudly talking about my latest project the interviewer asked to see my site. Without hesitation I began loading (Reach4urPhone) on the office computer. Then -BAM- all my styling was wrong! Padding seemed obsolete, margins were out, etc. Spluttering and trying to recover gracefully I finally brought it up in a more recent version of Safari that rendered my website in all its true glory. I knew it was a older browser issue but at the time couldn’t figure it out. Well I have now and I’m here to share.

Basically, older browsers that can’t recognize the new HTML5 tags don’t know how to display them. Some decide not to display the elements as block-level elements even though they are defined as block-level in the HTML5 W3C standard. To fix it, add this to your CSS: article, aside, figure, footer, header, hgroup, menu, nav, section { display:block; }. By setting display as block for the new and unrecognized HTML5 elements header, footer, nav and section you tell/force the browser to render them correctly instead of just guessing.

Thinking back I realized the new Visual Studio MVC3 Web Application project template (with the MVC3 Tools Update installed) with the “Use HTML5 semantic markup” checkbox checked actually has a default CSS file with the rule: header,footer,nav,section { display: block; }. Too bad I had noobishly removed it when decluttering.

- benmccallum

PS. I got the job :)

The Internet Explorer 6 Countdown

Just doing my bit to the cause. Spread the word and make life a tonne easier for us web developers… please :D

For a faster, safer browsing experience, upgrade to the latest version of Internet Explorer for free.

a foreach loop with an index thanks to LINQ

I’ve been spending a lot of time using ASP.NET MVC3 and the new Razor syntax. One of the newer things for me is to throw away the from the days of WebForms and throw the foreach loop straight into View template markup. Recently I needed to maintain an index of the current item when iterating through my Model so that I could add a class in the markup to every third item in the IEnumerable.

Sure, you could use a simple for loop like so:

@model IEnumerable

<ul>
for (int i=1; i<=Model.Count(); i++)
{
<li @if (i%3 == 0) { <text>class=”last” </text>}>
Movie Name: @Model.ElementAt(i).Title
<li>
}
</ul>

But the markup isn’t as readable and it isn’t completely clear that you are iterating over the Model elements until you look within the for loop. You are throwing away all of the advantages of the foreach syntax simply to gain the index of the element. Surely there’s is a better way you ask? Luckily there is thanks to the power of LINQ:


@model IEnumerable

<ul>
@foreach (var movie in Model.Select((x, i) => new { Data = x, Index = i }))
{
<li @if ((movie.Index + 1) % 3 == 0) { <text>class=”last” </text>}>
Movie Name: @movie.Data.title
</li>
}
</ul>

Now you can access the index via movie.Index and the strongly-typed Movie object via movie.Data. Plus the code is much more readable and consistent with the more commonly used foreach syntax.

Happy coding :)

jQuery live() function and Safari mobile version bug

Recently I came across an interesting problem. As you can now see on my website @ http://reach4urPhone.com/player/weapons?gamertag=xBLACK%20V3N0Mx I have a table displaying a player’s weapon stats in a table. By pressing the arrows above the columns the user can roll through varying columns to show more stats, an effective way of cramming more stats into a mobile interface.

To do this I simply wrote a click handler with the appropriate functionality and bound it to the element via jQuery’s live() function.


$('#rightArrow').live('click', function () {
//...
});

It was necessary to use the .live() jQuery method to bind my click handler to elements inserted into the DOM after the initial page load (ie. via AJAX). Everything seemed to be working fine on my desktop development machine in Chrome, Safari, Firefox and IE9. But when I deployed the site and viewed it on my iPhone and iPad the click handler was never called. After some digging I found this jQuery bug report: http://bugs.jquery.com/ticket/5677

It seems that the live jQuery method is never executed in the Safari mobile environment, or at least does nothing. So until the jQuery team handles this situation I’ve implemented the following workaround…
Instead of binding a function to the event, use the old-fashioned way by adding this to your clickable elements:
onclick="functionName(this)"

and refactoring your existing code into functions with the appropriate names of course and the structure function functionName(obj) { ... }

Note: Passing this to the function enables you to identify the element which was pressed so you can perhaps give a clicked appearance through CSS by doing
$(obj).css('...', '...')

Finally, I realise this is not the “Unobtrusive JavaScript” way but hopefully the jQuery team will implement a solution soon. Full solution is available on my site as of this post.

reach4urPhone.com BETA released!

bungie logo

Recently, I’ve been working on a mobile phone web application for viewing Halo: Reach stats. Bungie and Microsoft (developers and publishers of the critically acclaimed and awesome game) have been kind enough and forward-thinking enough to expose an API (application programming interface) for us developers to extract data about the game, from player details to game history and fileshares.

I’ll be talking more about this website in the future, including how I’ve designed and maintained it, but for now go and visit it @ reach4urPhone.com and let me know what you think. This site is built using ASP.NET MVC3, HTML5, CSS3, jQuery and I hope you like it!

-Ben

A New Beginning

Hi Interwebs,

It’s been a while since I’ve posted anything on this blog. I’ve been busy finishing my Honours degree :|

But now that is out of the way I am going to start posting things I learn on here. Whether they be in the ASP.NET MVC framework, ASP.NET Web Forms, C#, CSS3, HTML5, jQuery and other fields. I’m extremely excited about these new technologies and have made the shift from web forms to MVC3 so I’m going to share everything I learn and find useful on this blog, for your sake and my memory.

Regards,
Ben

Useful Resources for Looking at Library 2.0

I’ve been doing a lot of research lately into Libraries and their adoption of web 2.0 principles/technologies and also Enterprise 2.0. In my research I have come across some excellent information which I am going to share to those of you who are interested.

These resources all come from the Online Computer Library Centre website in the reports section. I cannot stress how complete, in-depth and useful these resources are. The OCLC team conduct huge environmental scans and collect masses of data on Library usage, social networking, emerging technologies, perceptions of libraries and so on with plenty of statistics and graphs to support their studies.

The website contains the following studies (of which can all be downloaded from the website in PDF format):
(Particularly useful documents to the Library 2.0 assignment for INB students are marked with *’s)

You can visit the reports homepage here.

Hope this helps others completing the Enterprise 2.0 RFP assignment as well as anyone looking for information and data to back up Library 2.0 implementations.

Post Navigation

Follow

Get every new post delivered to your Inbox.