Running an ASP.NET MVC 3 app on a web server that doesn’t have ASP.NET MVC 3 installed

Last week we released several new web products – including ASP.NET MVC 3.  We’ve had a bunch of great feedback and a ton of downloads since then.

One question a few people have asked me recently is: “My web hosting provider doesn’t yet support ASP.NET MVC 3 - any idea when they will install it?”

The good news is that you don’t need to wait for them to install anything on their web-servers.  As long as your web hosting provider supports .NET 4, then you can build and deploy ASP.NET MVC 3 applications on it today – without the hosting provider having to do anything to enable it.  The below post describes how you can enable this.

Some Background

We support two ways for you to install and use the assemblies that implement ASP.NET MVC 3 on a machine:

  1. Have the ASP.NET MVC 3 assemblies installed in a central place on a machine, and have web projects reference/use the assemblies from there
  2. Copy the ASP.NET MVC 3 assemblies into the \bin folder of your web project, and have your web project reference/use the assemblies from there

The first approach is the default approach we use with Visual Studio, and has the benefit of enabling us to easily service the ASP.NET MVC 3 assemblies using Windows Update (in the event of a bad bug).

The second approach is also fully supported, and has the benefit of not requiring ASP.NET MVC 3 to be explicitly installed on a machine in order for it to be used.  Instead you can just copy/ftp your web application onto a server (with the ASP.NET MVC assemblies contained within the \bin directory of the application) and it will just work.  You should use this second approach if your web hosting provider hasn’t explicitly installed ASP.NET MVC 3 yet on their servers.

Approach 1: GAC Based Referencing of ASP.NET MVC Assemblies

When you install ASP.NET MVC 3 on a machine, a number of assemblies are automatically registered in the GAC (global assembly cache) as part of the installation process.  The GAC provides a central place that .NET assemblies can be installed and serviced (via Windows Update).  Because it provides an easy way for us to update/service assemblies, ASP.NET MVC projects - by default - reference the assemblies that implement ASP.NET MVC 3 from it. 

If you copy a vanilla ASP.NET MVC 3 project – that uses the default approach of referencing the ASP.NET MVC 3 runtime assemblies using the GAC – onto a machine that has not had ASP.NET MVC 3 installed on it, then you will see an error message like below when you run your application:

image

The above error indicates that one of the assemblies required to run ASP.NET MVC 3 from the GAC cannot be found.  Installing ASP.NET MVC 3 on the machine will fix it and allow your application to be run.

Approach 2: \Bin Directory Based Referencing of ASP.NET MVC Assemblies

The alternative approach your projects can use is to distribute and reference the ASP.NET MVC 3 runtime assemblies from the \bin directory of your web application.  The benefit of this approach is that you do not need to explicitly install ASP.NET MVC 3 on a machine in order to run your web application.  Instead, you can just copy/ftp your web application onto an existing .NET 4 server and it will work.  This approach works for both “full trust” and “medium trust” scenarios – which means it will even work with low-cost shared hosting offers.  No addition registration/installation/action is required by the hosting provider in order to enable this.

Enabling your web project to “bin deploy” the ASP.NET MVC 3 assemblies is easy and only takes about 2 minutes to do.  Here are the steps to follow:

Once configured for \bin based deployment, an application you build with ASP.NET MVC 3 will work on any web server that has .NET 4 installed on it.  Just copy/ftp it up to the web-server and it will run.  You don’t need the hosting provider to do anything to enable it – this is true even for low-cost shared hosting accounts.  

Finding a ASP.NET Web Hosting Provider

You can browse the Windows Web Hosting Gallery to find good ASP.NET based Windows Hosting Providers (all of which support ASP.NET 4):

image

The Windows Web Hosting Gallery allows you to filter/browse hosting offers by different countries and service level offerings.  It also allows you to easily filter by “Shared Hosting”, “Virtual Hosting” and “Dedicated Hosting” offers.

“Shared Hosting” refers to an offer where many customers run on a single web-server and you do not have admin access on the remote server.  “Virtual Hosting” offers provide you with your own Virtual Machine image on a remote-server – typically with OS admin rights and remote terminal server access to manage it.  “Dedicated Hosting” offers provide you an entire physical server, and also typically provide OS admin rights and remote terminal support to it.

The prices for Windows based web hosting are now very cost effective, and start from as little as $2.75/month for a shared hosting offer and $23/month for a virtual hosting offer.  All of the above offers are capable of running ASP.NET MVC 3 using the \bin deployment approach.

Summary

You can deploy ASP.NET MVC 3 based applications to any web-server that has .NET 4 enabled on it – without having to wait for the hosting provider to explicitly install ASP.NET MVC 3 on it.  Simply use the steps above to convert your project to use \bin based deployment of the ASP.NET MVC 3 assemblies, and then you can copy your application onto any ASP.NET 4 based server and it will just work.

Hope this helps,

Scott

P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu

18 Comments

  • Good post at right time. I was just wondering about the deployment and got your twitter update this post. Thanks

  • It's a good thing to _only_ have BCL libraries into the GAC. Everything else is project related and as such belongs into the bin of the project.
    I often found webservers with different versions of log4net (or other components) into the GAC. Then when they deploy their project to the production server they get error messages about version mismatches and stuff and then the come wining at my desk..

    First thing I usually do is make a copy of the virtual server and remove anything from the GAC that isn't part of of the system.* namespace. And put the assemblies into the projects bins. After a few hours (when I have checked all websites on the server) I put the new virtual machine into production and everything is running like a charm.

  • MVC3 rocks and I really love it.
    I have a question though.
    I was used to use web deployment in order to get my aspx files (until mvc2) compiled in dll and their content replaced by the dummy statement "do not delete....". I prefer this strategy when I want to post my web app on a shared hoster.
    This does not seems to work for Razor files .cshtml.
    Are you planning to support this for Razor also in the web deployment feature of VS2010.( I read arecent article from Phil Haack where he compiles Razor templates into dll for better reusability )

  • You said "You can deploy ASP.NET MVC 3 based applications to any web-server that has .NET 4 enabled".

    Is it true for a Windows Server 2003/IIS6 that we had installed ASP.NET 4?

  • Great post! I am so amazed you can make time to provide this wonderful information.

    I just had issues with an MVC 2 website on GoDaddy, where Tech Support was confused by my website. It did not "look right." Good news for Microsoft: I moved that project to Azure, where things just run and my only concern is controlling costs.

  • I am new to MVC. This is really helpful as it eliminates the dependency of software installation on the hosting machine.

  • Hi,

    It turns out that you have to xcopy the following files in order to host the razor view engine:

    1. System.Web.Razor.dll
    2. System.Web.WebPages.dll
    3. System.Web.WebPages.Razor.dll
    4. System.Web.Helpers.dll
    5. System.Web.WebPages.Deployment.dll
    6. Microsoft.Web.Infrastructure.dll

    The interesting thing is the 4,5,6, even I am not using the new Webhelpers this file is needed the same goes for the last two, my initial thought was the last two was required to deploy from the webmatrix ide, but it proved me wrong.

  • Hi Scott, I am just confused about the future of Webforms because Microsoft is not giving clear signals about it. Microsoft says that they are heavily investing in webforms but I cannot see the results yet. Even Asp.net 4.0 was more about cleaning up the things rather than adding up new things. Looks like we will eventually HAVE TO SWITCH TO ASP.NET MVC unless Microsoft gives a strong and clear message about webforms.

  • Hi Scott,

    Can the same thing be achieved for SQL Server Compact Edition 4?

  • Will you be discussing this in detail at the upcoming MvcConf 2?

  • I am just confused about the future of Webforms because Microsoft is not giving clear signals about it. Microsoft says that they are heavily investing in webforms but I cannot see the results yet.

  • Thanks Scott. As Kazi Manzur Rashid mentioned, those additional dll's listed are required if you don't have MVC 3 loaded on a .NET 4.0 loaded win 2k8 SP2 server. Thanks Kazi for that info.

  • Right now I'm trying to wrap an MVC3 solution in a CWAB (Composite Web Application Block) solution. Someone else in my company successfully deployed a CWAB/MVC2 solution with some hacking of the System.Web.Routing.dll (he ran into a problem with Areas I believe).

    I'll let you guys know if I'm successful or not (since we also don't have 4.0 running on the production servers, yet. For demos I've been having to create separate application pools in IIS for 2.0/3.5 and 4.0 apps on my local dev server).

    But yeah, I just wanted to throw that out there into the universe. Maybe the Coding Gods will shine light upon me so I can finally make a worthwhile push to get the MVC pattern into production in one of our big apps :)

  • Wonderful news!

    So, to get this straight: the company that hosts my web sites only need .NET Framework 4 installed and no additional MVC-stuff?


    //Marcus

  • @Marcus,

    >>>>>>>> So, to get this straight: the company that hosts my web sites only need .NET Framework 4 installed and no additional MVC-stuff?

    Yep - that is correct.

    Thanks,

    Scott

  • @Justin,

    >>>>>>>> I have mvc 3/razor running perfectly locally, but when I publish the code to my windows server 2008 box, it gives me the error:

    >>>>>>>> Method not found: 'System.Web.Razor.GeneratorResults System.Web.Razor.RazorTemplateEngine.GenerateCode(System.IO.TextReader, System.String, System.String, System.String)'.

    Can you send me email (scottgu@microsoft.com) about this? We can then help follow up and investigate.

    Thanks,

    Scott

  • It would be great to have a NuGet package with MVC3 bin deployment assemblies and setup.

    Any chance of seeing something like that?

  • I'm glad I ran into this article, I thought I was going to be stuck with MVC2 (not necessarily a bad thing). I'm looking forward to using the new Razor view engine. The syntax is a lot less tedious.

Comments have been disabled for this content.