Category Archives: Code

Software Development

Serving Asp.Net MVC4 pages on Mono with xsp. That is, with xsp4

I couldn’t see why the command-line xsp command didn’t work until I realised there is an not-very-well-advertised xsp4. At which point I guess the 2 in xsp2 means .Net 2 rather than, say, xsp version 2. So now I know that:

To run an Asp.Net 4 or 4.5 web application you need xsp4; whereas to run an Asp.Net 2 (or 3 or 3.5) app you need xsp2.

man xsp4 returns nothing, but the switches all seem to be the same as you’ll find with man xsp. So to run your MVC4 web app from the command line, cd to the web app directory and just run xsp4.

Asp.Net MVC 4 on Mono and Windows

Having at last got an Asp.Net Mvc 4 project template working on the Mac/Xamarin/Mono, I came to grief again when deploying it to a Windows hosted server. The problem is that whereas mono works without Microsoft.Web.Infrastructure.dll (and indeed is likely to give errors if you have it), for hosting on Windows you must re-include it.
So my deploy process now includes putting Microsoft.Web.Infrastructure.dll back in the web application bin/ directory; and now I can develop on the Mac and deploy to a Windows Server.

Use P4Merge as the Merge Tool for Mercurial Windows

Whenever I do a Mercurial merge on Windows I get a little Visual Studio Dialog popup saying File -nosplash not found. Which is annoying. Especially as I have Perforce’s excellent p4merge installed.

You can set the merge tool used by Mercurial like so:

Edit %userprofile%\Mercurial.ini

where Edit is your favourite text editor, and %userprofile% is usually C:\Users\username\.
Then add this section to your Mercurial.ini file:

[merge-tools] 
p4.priority = 100 
p4.premerge = True
p4.executable = C:\Program Files\Perforce\p4merge.exe
p4.gui = True 
p4.args = $base $local $other $output

You can change “p4” to anything, so long as you change all occurrences of p4 left of the = signs.
If p4merge.exe is in your path and if you use p4merge as your identifier, then you don’t need the executable line:

[merge-tools] 
p4merge.priority = 100 
p4merge.premerge = True
p4merge.gui = True 
p4merge.args = $base $local $other $output

MVC Html.ActionLink() fails weirdly with routeValues parameter in Asp.Net

It seems simple. In an MVC view page you have an @Html.ActionLink with parameters for action, controller & route values:

@Html.ActionLink("Some text","Action","Controller", new {id=1})

but instead of rendering the text with a link to the action, it renders a link with some weird URL that isn’t what you want at all.

If you use intellisense to look at the overloads for ActionLink, you’ll see that the overload you’ve got is interpreting new {id=1} as html attributes not as route values. You need the overload with one more parameter, which can be null:

@Html.ActionLink("Some text","Action","Controller", new {id=1}, null)

And then it works.

.Net Assembly Binding Redirect doesn’t work – because you have an Uppercase/lowercase error in config

Thanks to Eran Stiller for spotting the fact that assembly binding redirect fails—with no appropriate error message or clue as to the reason for failure—if you used PascalCasing instead of camelCasing casing in your config.
This fails:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Moq" Culture="neutral" PublicKeyToken="69f491c39445e920" />
        <bindingRedirect oldVersion="4.0.10827.0" newVersion="4.1.1309.1617" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

because of incorrect case in the attributes Culture and PublicKeyToken. Make them camelCase like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Moq" culture="neutral" publicKeyToken="69f491c39445e920" />
        <bindingRedirect oldVersion="4.0.10827.0" newVersion="4.1.1309.1617" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

and now it works.