I have moved my active blog over to tumblr. I've maintained this blog for reference but will be posting to http://www.robustsoftware.co.uk instead. I've pointed my Feedburner feed to tumblr so if you're subscribed already you should already have switched with me.

ASP.NET MVC: Strongly typing form methods with input values

Recently I've had the opportunity to use the latest MVC drop to create a web site that is more than just a play thing. Along my travels I found an annoying problem with using the Html.Form helper. I wanted to call this action:


If I wanted to let the framework automatically map values from my form to the action's parameters I was unable to strongly type the action URL using the Html.Form helper. This forced me to use code such as:


Now this meant that I was heavily dependant on strings to tie everything together properly. The controller, action and form value mapping was dependent on it. This felt a bit nasty, but it worked.

The reason I could not use the existing strongly type lambdas was that the action required arguments and in order to use the existing lambdas you have to supply arguments for all the parameters. This is possible with for the Verify action by supplying dummy values:


This would work for my simple example. However, it relies on the userName and password arguments not being mapped in your routing rules. If you wanted a value such as id to be selected from a drop down using this method would hard code in the id which isn't what you want.

I thought there must be some way round this using a combination of generics, reflection and LINQ so I used the existing generic method as a starting point and got an understanding of how it used Expressions and Funcs to tie things together. From this I figured there must be a way to create an expression pointing at my action which I'd then be able to interrogate to grab the values I wanted.

At this point I must thank Daniel Cazzulino and Ayende Rahien for their fantastic posts on getting methods from expressions and static reflection respectively. These helped me work out the code which did the hard stuff without having to know absolutely everything about Expressions and Funcs.

Through the use of an Expression we can create a form helper which looks like this:


Though quite verbose, this has allowed us to strongly type the action to be called and the type of its parameters (this will generate a URL for the form of /Home/Verify). I believe this type safety is a great improvement on the bunch of strings we started off with.

Here's how the magic happens:


It works by taking the Expression that is passed to it to get a MethodInfo object which has the details of our Action. From the MethodInfo object we can grab the name of the action and the name of its controller, passing it in to the existing Html.Form helper method. Through a generic constraint we can ensure that this method can only be used for an IController and that the method called must return an ActionResult object.

From this single method it is simply a case of writing a bunch of overrides so that you can have all the control over the form available in the existing helpers (i.e. setting the form method and passing in a dictionary of values). There is a limit of 3 on the number of parameters that this can currently handle due to the limit on arguments that a Func can take. There may be a way to work around this but it is beyond my needs so I have not looked into it.

I couldn't find a decent way to host the source so drop me a line if you want it, or even better let me know where I could host it for free without people having to suffer pop ups or general eye spam.

File.Create and the mysterious IOException

I was putting together a bunch of unit tests today for a program that's managing the transfer, etc. of a bunch of files. I created an extension method to make it easy to add empty files to directories for my tests (code requires the System.IO namespace to be imported):

public static void CreateEmptyFile(this DirectoryInfo directory, string fileName)
    File.Create(Path.Combine(directory.FullName, fileName));

However, everything was going fine with my tests until I started trying to move files about. Whenever I wanted to move a file I would get an IOException with the message "The file is already in use". My files were being created as I expected, just I could not move them without an exception being thrown. Going over and over the code I could not see where the problem is coming from.

Eventually after bashing my head against a wall for a while I decided to go over everything from the beginning. What I had missed is that File.Create is not a method but a function which returns a FileStream. So the problem was that this FileStream was left hanging around and so I could not do anything with the file.

Once I had realised that the fix was easy, close the stream:

public static void CreateEmptyFile(this DirectoryInfo directory, string fileName)
    File.Create(Path.Combine(directory.FullName, fileName)).Close();

Hope this helps someone see the wood from the trees and not waste time looking for a simple mistake.