Today I thought it be cool to create a web site on Azure and use all of the great new capabilities of MVC3 and Razor. So, I fired up Visual Studio 2010 and unfortunately there were no templates that allow you to do such a thing. Well then, knowing that you can “bin deploy” MVC 3 apps on servers that don’t have MVC3 installed on them by including the right binaries I figured I would give it a go anyway. Here’s what I did and this may look like a lot of steps but I was able to do this all in the span of about an hour sitting at Hooters, coding, talking to some friends and having a beer.
- I created 2 solutions in 2 different folders. One to create the Azure setup (I just used the existing MVC2 template to get things started) and a separate one with MVC3 using Razor as the view engine.
- In the Azure solution, I deleted all of the common MVC folders (Content, Controllers, Models, Scripts and Views) to get them out of the way
- In Windows Explorer, I copied those same folders from the MVC3 project over to the Azure project (the MVC2 and MVC3 folder structure is consistent)
- Back in Visual Studio (in the Azure project), I clicked on the “Show All Files” icon so I could see the folders and files
- Right-click on each of the folder names that I had previously deleted then copied and selected “Include in Project”. All of the subordinate files are included automatically.
- Now, to help make sure that the MVC3 assemblies are available for the application, I created a “Binaries” folder in the Azure project. Thanks to Scott Hanselman’s Post and Reflector I found out which assemblies to copy and from where. Copy all of the assemblies on the list to the Binaries folder you just created. Here’s the list:
- System.Web.Mvc.dll (v 3.0.0.0) and System.Web.Helpers.dll are the two assemblies that the application needs direct references to. The rest of the assemblies are dependencies of those two.
- Microsoft.Web.Infrastructure.dll
- System.Web.Razor.dll
- System.Web.WebPages.dll
- System.Web.WebPages.Razor.dll
- System.Web.WebPages.Deployment.dll
Mvc can be found here: Depending on your system (Program Files or Program Files (x86))\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
The rest can be found here: Depending on your system (Program Files or Program Files (x86))\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies
- Make sure the “Build Action” on the dll files in the “Binaries” folder is set to “None” on all files
- Remove the existing reference to “System.Web.Mvc” since it is pointing to version 2 and we want version 3
- Add references to all the assemblies and set the “Copy Local” property of each to “True”. Worth noting: The application really only needs references to a couple of the assemblies (mainly Mvc and Helpers) so it is arguable that you should only reference those two in the app and the installation process should make the rest of the assemblies available rather than using the VS project do your dirty work. I tend to agree with that and in a production setting I would subscribe, however, for the purposes of this post, and to ease the deployment to Azure, I added all 7 of the references directly in the project and set them all to copy local.
- Modify Web.config (this is easier doing a side-by-side compare with the original MVC3 app and the Azure app config files)
- In “<system.web><compilation …><assemblies>” change the version number of “System.Web.Mvc” to “3.0.0.0
-
Add “<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />” and “<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> to the list of assemblies. So, ultimately, it will look like this:

-
Make sure <pages><namespaces> looks something like this:

-
Finally, for the sake of being thorough, change the assembly binding at the bottom of the config so that all references point to MVC version 3

Your MVC3/Razor app should now be runnable within the local Azure development environment. I’m not going to cover it in this post but if you add a certificate and appropriate credentials, you should be able to publish the app directly to a live Azure site.