Tuesday, 23 March 2010

ASP.net MVC – Exe download and file versioning

We use an application at work which is supplied as a single exe file which is downloaded from their website. The issue is the file has always been called application.exe irrespective of the version. With each new version it just gets overridden and keeps the same file name.

We added a request to the manufacturer a request that the file name include the version so we a) know which version we are getting and b) can keep a couple of historical versions locally just in case. However; due to the length of time they have gone for the single filename it is now linked from many different places and they don’t want to break the links.

It got me thinking, this would be ideal for a route in Asp.Net MVC but can you set the route to accept a string “application.exe” and it do as required. The short answer is yes! :-)

The thinking was setup the route for the single filename. This would execute an action on a controller which would go off to a repository, find the latest version file and then return the file as a DownloadResult to throw up the save file dialog. I had a hunt round the internet and found an oldish post by Phil Haack for this kind of thing. I updated it to map to a specific file, for instance on a file share.

public class DownloadResult : ActionResult
public string FileLocation { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }

public override void ExecuteResult(ControllerContext context)
if (!String.IsNullOrEmpty(FileLocation))
"content-disposition", "attachment; filename=" + FileName);
= ContentType;


Once you’ve got that sorted all that’s left is to add in the route in your Global.asax …

routes.MapRoute("download", "application.exe",   
new {controller = "Home", action = "Download"});

And tad ta :-)

The other options would be routing / redirecting at the IIS/Apache level, but as a developer this seemed to be the easiest and simplest way to enable data access to help with new version releases.

Wednesday, 17 March 2010

SqlParameterCollection Extension Method AddWithValue

I don’t know why it’s not included in .net 3.5 / c#3 but the recommended way of adding a SQL Parameter to a SqlParameterCollection is to use the AddWithValue method; however, you can’t specify the SqlDbType with any of the overloads. It’s not hard …

    public static class DataAccessExtensions
/// <summary>
/// AddWithValue specifiying the <see cref="SqlDbType"/> of the parameter
/// </summary>
/// <param name="collection">The collection to add the parameter to</param>
/// <param name="parameterName">The name of the parameter</param>
/// <param name="value">The value of the parameter</param>
/// <param name="sqlDbType"><see cref="SqlDbType"/> which the SqlCommand is expecting for the created parameter</param>
public static void AddWithValue(this SqlParameterCollection collection, string parameterName, object value, SqlDbType sqlDbType)
var result
= new SqlParameter(parameterName, sqlDbType) { Value = value };

Wednesday, 10 March 2010

Why do you tweet?

When Twitter came out I didn’t really see the point. As it gained in popularity I still didn’t really see the point; other than using up bandwidth. I’m not a big fan of social networking sites. I hate Facebook for a start. I think the layout is counter productive, it changes too often and generally the “apps” it has are pointless. The only reason why I’m signed up to it is some of my friends seem to have lost the ability to use email, in the traditional sense, and it’s the only way to communicate with them anymore.

Anyway, I digress …

After moving jobs back in Oct ‘09 and settling into my new role I started using Asp.Net MVC more as well as other technologies. After reading some of my regular blogs the writers started putting things like “Also follow me on twitter to get up to date posts, thoughts etc.”, so I thought why not; so I signed up.

The next question which hit me was which client to use? Well due to numerous recommendations from friends I decided to go with TweetDeck. It’s great and does what I want it to! It now just sits in my system tray and pops up every so often with little notes :-)

So the main reasons why I tweet?

- To follow some friends
- To follow .Net bloggers to links, trends etc.
- To follow Formula 1 people to keep up to date with the goings on in the sport.
- To follow some diet/health people to help with the WeightWatchers.
- Following on from the last 3 points I subscribe to a few hashtags (#f1 and #aspnet being a couple) for technology, disc golf, formula 1 and Weight Watchers.

And thats it!

I wouldn’t have found a number of blog posts and interesting details without Twitter over the past few months. The other part is the inside information about Formula 1 in the close season which before has not been possible to access, but with more and more drivers and staff using it, it’s been brilliant.

So if you’ve not thought about it before; Why do you tweet?

Monday, 8 March 2010

February Update - “And the winner is … “

… beer this month!

Short update this month;

- Not much lost
- Big beer in take as it was a mates Stag do

Upside; Spring is on it’s way. It’s sunny, although cold, so can go and play disc golf after work. The summer is on it’s way! :-)

Current weight loss (since Aug ‘09) : 16 1/8lbs

Current Status : Bad! (-97.8)
Beers: 490 + 37 = 527
Miles: 183 (gym: 129.5; road: 33) + 7.5 = 190.5
DG Rounds: 75 + 8 = 83
Gym visits: 94 + 5 = 99
Lengths : 56.7 (total: 567) 

Good => Exercise > Beers
Level => Exercise == Beers
Bad => Exercise < Beers

Wednesday, 3 March 2010

Asp.net MVC – When to use strongly typed ViewData?

In short; all of the time unless you have a *really* good reason not to.

With the use of generics being able to specify a strongly typed model per view / partial view is so easy with the baked in functionality in the framework, so why aren’t you using it?


The whole point of MVC is to have control over markup, enable specific routes and reuse existing views / partial views in different locations among others. It’s not to make your life harder than it already is. The underlying issue is if you don’t have the a strongly typed view model for each view then you don’t have confidence in having complete control over it. Throw in a dev team, potentially, changing the same weakly type code and you’re looking for pain.

When you first start looking at examples of MVC applications, you find examples where people start putting in values in to the ViewData dictionary which is available on the views/controllers.

ViewData["MyKey"] = "Some string value";

This uses the weakly typed ViewDataDictionary which can lead to issues with keys not being present, casting it to the wrong type (potentially) and can lead to unnecessary complexity beyond what is required. This can create hard to maintain spaghetti code in the view markup (can anyone say “classic asp 3” ?) … all of which is bad.

So are you suggesting a ViewData model class per view?

Yes; this is exactly what I’m suggesting. This leads to well structured solutions and keeps the code and views together. This isn’t such a big problem when working on your own, but if you know the system you are writing will be a lot bigger when completed or you work in a bigger team (or both) then it’s better to keep things as simple and clear as possible as early as possible. Set a good foundation to work from.

So my current Visual Studio project layout looks something similar to this :


The view data class definitions and the associated full / partial views are filed away in similar namespaces / folder structures * underneath their associated areas of the solution so that you know which are linked together more easily.

* The views in this case are filed under Document instead of Documents because of the name of the controller, although are potentially incorrectly named  :-)


So to summarise, why do this? Well it helps to know what you’ve got to play with in each view. It breaks down the specifics which you need to pass into a view/partial view to get it to operate nicely. It also means when re-using partial views you know what to pass into them. Also, with strongly typed goodness it helps with refactoring especially when using tools such as Resharper.

Let me know your thoughts.

Further reading:

MikesDotNetting - ASP.NET MVC Partial Views and Strongly Typed Custom ViewModels

Thursday, 4 February 2010

Best error message … Ever!

I’ve had a few good error messages in my time, but waiting for something I am debugging at work to time out I just received the following message:



Edit: Found a useful Stackoverflow answer to help resolve this issue.

Monday, 1 February 2010

January Update; it’s all about the diet & disc golf

It’s time for a January update on how the diet/exercise bits and bobs are going.

Generally speaking January has been a good month. Loosing every week except the last week and then only putting on 1.5lbs that week. This week was a big one as weeks go as I had a dev social on the Friday after weigh in the week before. We then went out for a curry on the Saturday night. The following week Lau and I had our “get together” anniversary (9 years!) which also doubles as our 1/2 yr wedding anniversary (1.5yrs) and my birthday on the Thursday so went out for dinner on Wednesday. As you can image this didn’t help with getting to the gym or dieting generally.

So overall, I have lost 1.25lbs this month. This isn’t great and I need to step it up a bit in February. I really want to get under 11stone by end of Feb / mid march time. So 4-6 weeks to drop 4.5lbs so its defo do-able.

So that’s it diet wise. Along with Frostbreaker the last weekend of the month where I shot reasonably well (for me) and was only 5 shots off making the Adv Am final. This is a good achievement for me and I’m really happy with how I played. I also had loads of energy which resulted in playing another 7 or so holes after lunch while the finals where taking place but also ran up a couple of hills. The gym’ing must be helping!

Right, so end of month means first thousand beers update …

Current weight loss (since Aug ‘09) : 14 5/8lbs

Current Status : Bad! (-83.3)
Beers: 461 + 29 = 490
Miles: 162.5 (gym: 129.5; road: 33) + 20.5 = 183
DG Rounds: 67 + 8 = 75
Gym visits: 82 + 12 = 94
Lengths : 54.7 (total: 547) 

Good => Exercise > Beers
Level => Exercise == Beers
Bad => Exercise < Beers

Tuesday, 26 January 2010

Migrating Team Foundation Server 2010 Beta 2 to new servers

Disclaimer : It worked for me, and I hope it works for you, but do so at your own peril. Make sure everything is backed up and I’d suggest testing on a spare machine first before doing it for real.

We’ve been looking at changing our infrastructure at work and moving from VMWare for our virtual servers to using Windows Server 2008 and Hyper-V. The main server which needed to be moved/converted/migrated between the two different virtualisation technologies was the TFS Server; and hence my domain.

I started off by trying to convert the vmdk file to a vhd file which seemed to work well and could be mounted and the data on it read. However; I was unable to boot from it. After spending, way, too much time trying to get it working I decided to look at the migration exercise. And to be honest it was essentially a disaster recovery exercise as we’d have to go through the same steps if it all went wrong so the time wasn’t wasted.

So after doing a test run on a spare virtual machine we had before the server was taken down I fired up the install for SQL Express and started on the path to setting up TFS. After doing some Googling, and finding loads of different articles which seemed to do bits but not all of it I thought I’d post my findings to hopefully help someone in the future. These are the steps which I came up with. I have also now deployed TFS into the new virtualised production system and it seems to work fine (not got automated builds working yet tho).

Here we go:

  1. Backup databases from old TFS. This includes the Tfs_Configuration databases and any project collection databases.
  2. Build/patch new VM with Windows Server (or other OS)
  3. Install SQL Express 2008 with tools
  4. Restore databases, with the same names, into your new SQL Server instance
  5. Install Team Foundation Server 2010 Beta 2 (and reboot when required)
  6. Run the following command – making sure that you have the correct details in the right places

    c:\Program Files\Microsoft Team Foundation Server 2010\Tools>tfsconfig accounts /add /accountType:applicationTier /account:"NT Authority\Network Service" /sqlInstance:.\SqlExpress /databasename:tfs_configuration
  7. Create a local user named after the server name. So for example if your server is called “TFS” then create a local user called “TFS$” (without quotes)
  8. Fire up the Team Foundation Server admin, go to configure installed components and run the Application Tier only upgrade wizard
  9. Once in the wizard point it to the SQL instance locally and it should find the configuration database, select it and continue.
  10. Finish the wizard and it should be pretty happy. Next thing to do is update the server urls on the main server details page to point to the new server name (as it’ll have the old server name in it)
  11. Start up the project collection(s) and throw in a server restart for good measure.
  12. Done!

You should now be able to connect to the server through Visual Studio as before. You will need to add in the new server details (and remove the old). I found all the workspaces where fine and all was good to go.

With a big team its probably wise to get everyone to shelve all their changes over a weekend or evening and make sure nothing is checked out. But this is down to preference and may need to be tested.

Hope this helps someone in the future :-)

Saturday, 9 January 2010

Cheap remote backup solution

After reading the madness of what happened to Phil Haack a few weeks ago and a few other people who had VPS’ running on the same machine after a physical hard drive failure everyone on the box (so it seemed) realised that they didn’t have much, if any, backup solution in place. This got me thinking …

There are a number of remote backup solutions for remote servers but they cost money which at the current time I don’t have, and most people don’t have due to the current economical climate. So was checking my email the other day and scrolled to the bottom of the page and noticed that I was only using a small amount of the my gmail account storage. It got me thinking that there was a plug in you could get to use your gmail account a remote storage / mapped network drive. This could be the reliable backup solution I’m looking for.

After doing a bit of Googling about to see if there was anything out there before I set down to write my own solution to this problem I found backup2Gmail on Code Project. Once I realised that you could run this from a command line then I was on to a winner.

So the backup bat file goes along the lines of:

Backup the svn respository using svnadmin to a backup folder (c:\backup\svn)

Checkout the latest code from the repository to a folder in the backup folder (c:\backup\code)

Run the backup 2 gmail command line to backup the c:\backup folder. It then zips it up and sends it to my account.

And that’s it. Put all that into a bat file, put some error checking in to make sure the dirs which are required are there at the beginner, removed at the end etc. and then set it as a scheduled task for some crazy time early in the day (currently set at 3am).

Hope this helps someone else in the future to avoid the heart ache of server failure.

Thursday, 7 January 2010

Map network drive in code

I needed to programmatically map a share drive to access files for a asp.net web application. This works on the app pool identity and doesn’t have mapped drives by default so after a little Googling, I found this :


It maybe a few years old, but works like a dream!

Wednesday, 6 January 2010

A device attached to the system is not functioning

I rebooted my windows 7 machine yesterday and could successfully remote desktop to it. I then disconnected from the session as I had some things running and wanted them to continue to run. When I try to re-connect tonight I get :


Any ideas what this means?

Edit: Tried running remote desktop from the command line with the /console option and that doesn’t resolve the issue either :-(

Friday, 1 January 2010

End of 2009 – General thoughts, musings and diet updates.

Well the end of the year is upon us and I think it’s time I reflected a little on what’s happened diet wise, fitness wise and a little in general.

First time in a while the exercise has been above and beyond the beer intake. With this and the diet that I’m undertaking with my wife the weight has gone down slowly yet steadily which is good. At least this way I am more likely to keep it off. I’m also feeling a lot healthier now that I have lost some weight and generally eating better. Improving what we ate (using Weight Watchers cook books) the food has been filling, tasty and yet not fatty or loads of points.

At least once a winter period I go down with a stinking cold, I don’t get the flu as I have the inoculation every year, and it hits me for 6 for a few weeks at a time. Since going to the gym regularly and eating better I have felt great for the past few months not being worse than a bit of a blocked nose, until now. Ah well, hopefully will start feeling better when I get back into the swing of things with gym, food and work on Monday.

I’ve been impressed that I’ve managed to keep the weight off as well and I feel good for it. Only downside is the bits they don’t tell when you’re on a diet like you’ll feel the cold more as you don’t have so much podge to keep you warm! Below is the graph of weight loss since the start of the diet. As you can see it’s been a bit poor over the festive period but will get it back on track in January.


As for work related stuff, it’s been up and down this past year as I’ve posted before, but I am looking forward to the new year and what it will bring. I’ve got a lot of things planned both work and external related and I look forward to implementing most if not all of it. Also in the new year I will be posting more disc golf related content as well as more coding and design posts. I hope you enjoy!

Right, last bit to do is the first thousand beers update …

Current weight loss (since Aug) : 13 3/8lbs

Current Status : Bad! (-94.8)
Beers: 386 + 75 =  461
Miles: 97.3 (gym: 64.3; road: 33) + 65.2  = 162.5
DG Rounds: 62 + 5 = 67 
Gym visits: 70  + 12 = 82
Lengths : 51 (total: 512) + 35 =  547 => 54.7
Good => Exercise > Beers
Level => Exercise == Beers
Bad => Exercise < Beers