ASP.Net MVC Projects running on Windows Azure

[For more recent information on using ASP.NET MVC with Windows Azure please see this post.]

Before you get started with ASP.Net MVC and Windows Azure – please install this hotfix.

Strictly speaking, ASP.Net MVC projects are not supported on Windows Azure.  That is to say that we haven't spent the time to fully test all of the MVC scenarios when running on Windows Azure. 

That said, for the most part, they do work, just as long as you know what tweaks you need to make in order to get up and running.

I've attached a sample application that Phil and Eilon on the MVC team put together to help make it easier for you to get started.

I’ll walk through the changes:

1. Start by creating a Blank Cloud Service.  File –> New Project –> Visual C# –> Cloud Service –> Blank Cloud Service.  I call it MVCCloudService

image

2. Right click on the solution node in Solution Explorer and select “Add New Project”

image

3. In the Add New Project dialog, navigate to the Web node under Visual C# or Visual Basic and select the ASP.Net MVC Application (I call it MVCWebRole). 

image

4. In Solution Explorer, right click on MVCWebRole and select “Unload Project”

image

5. Right click again on MVCWebRole and select “Edit MVCWebRole.csproj”

image

6. Add <RoleType>Web</RoleType> to the top PropertyGroup in the project file.

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
{. . .}
    <RoleType>Web</RoleType>

7. Reload the project, be sure to save the project file.

image 

8. If you want to use the Windows Azure runtime library, add a reference to Microsoft.ServiceHosting.ServiceRuntime.dll by right clicking on the References node and selecting “Add reference…”.  Scroll down in the .Net Tab and you’ll find it.

image

9. Right click on the Roles node in the MVCCloudService project and select Add –> Web Role Project in solution…

image

Select the MVCWebRole project.

image

10. Set Copy Local = true on the MVC DLLs:

  • System.Web.Abstractions
  • System.Web.Mvc
  • System.Web.Routing

These assemblies are not available when running on Windows Azure and need to be deployed with the Service Package.

Expand the References node in the MVCWebRole and right click on System.Web.Abstractions and select Properties.  Change “Copy Local” to true.  Repeat for the other DLLs

image

With these changes, the edit, build, debug, publish and deploy functionality will all work in Visual Studio with the Windows Azure Tools for Microsoft Visual Studio installed.

That said, it is still "use at your own risk".

Note: The sample has not been modified to use the Windows Azure ASP.Net provider (for example, the membership provider), stayed tuned.

The sample project is attached to this blog post.

MVCCloudService.zip