Building and Running ASP.NET MVC4 Apps With Mono, Xbuild, Nuget, and Xsp4

This post describes how to build and run ASP.NET MVC4 applications using Mono, xbuild, nuget and xsp4.

Following on from my post about building servicestack apps with Mono and friends, I wanted to get ASP.NET MVC apps up and running with Mono and xbuild too.

I ended up doing it with MVC4, as with MVC4 all the MVC-related libraries can be pulled in via Nuget.

I went down the route of compiling Mono 3. I’m not entirely sure if MVC4 works with 2.10.8, which is the version in most package repositories at the moment. I know for a fact it works with 3.0.10, as this is the version I built!

Compiling Mono 3

I followed this excellent post for compiling Mono and libgdiplus and xsp4. It all went fairly smoothly, other than the gotcha with libgdiplus as mentioned in my previous post.

Setting up a test project

erikd also handily provides a test MVC4 project, as bundled up from an empty VS2012 MVC4 project. It’s linked to in his post.

Like with servicestack, I’m hoping to set up a bootstrappable project template for MVC4 on github when I get chance.

Setting up Nuget package restore

Where I had to branch out from erik’s post is for actually building the project. erik doesn’t mention how he’s doing this, but I’m guessing that it might be with MonoDevelop or Xamarin Studio, which probably just works. I’m using xbuild from the command-line however, so there’s a bit more extra setup I had to do.

Nuget

The project already has packages.config for the required libs, and the solution has the required .nuget folder. However we need to tweak the Nuget.targets file as I mentioned in my earlier post.

Add a RestorePackages tag and an import to NuGet.targets using the following:

HelloWorld/HelloWorld.csproj
1
2
3
4
  <RestorePackages>true</RestorePackages>
  ...
  <Import Condition="HasTrailingSlash('$(SolutionDir)')" Project="$(SolutionDir).nuget\nuget.targets"/>
  <Import Condition="!HasTrailingSlash('$(SolutionDir)')" Project="$(SolutionDir)\.nuget\nuget.targets"/>

(See this post and comment).

I also found that I needed to remove a trailing space in the RestoreCommand setting in NuGet.targets:

.nuget/NuGet.targets
1
2
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir "$(SolutionDir) " </RestoreCommand>
                         --------->-------------->------------>----------->--------->---------->--------->--------->--------->-------->-------->------->------->-------^ this

Building

Something I forgot to mention in the previous post, before you build you need to do:

1
export EnableNuGetPackageRestore=true

It’s worth putting that in your shell startup script if you’re doing a lot of work with Mono.

I also did:

1
2
cd /usr/local/lib/mono/xbuild/Microsoft/VisualStudio
ln -s v9.0 v10.0

for good luck (and to resolve a build warning).

Then build with

1
xbuild

It should pull in all the required packages and build.

Then simply switch into the project directory and run xsp4.

1
2
cd Mvc4Base
xsp4

Browse to localhost:8080 and you should see your brand new MVC4 site!

Comments