We believe that some of you get this error when deploying your ASP.NET MVC 5 to shared hosting:

[FileNotFoundException: Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]

Previously, we have written blog about how to deploy your MVC to shared hosting environment. In this tutorial, we will be more focusing on above error message.

This is also one of a few component libraries that are needed for deploying an MVC application:

  • System.Web.Helpers.dll (required by the web.config)
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • Microsoft.Web.Infrastructure.dll

The system libraries are installed with .NET 4, however, 'Microsoft.Web.Infrastructure.dll' is only installed when Visual Studio is installed on the machine.  Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application - and we'd like to do so automatically.

There are a few ways to automatically deploy the 'Microsoft.Web.Infrastructure.dll' component library with your application.  The steps depend on which version of Visual Studio you are using.

1. Right-click on your project and select "Add Deployable Assemblies" and you'll see the following dialog:

2. When deploying for MVC, only choose the first option.  Never mind the second option even though it says "Razor syntax." The second option is for deploying the required libraries for projects officially known today as ASP.NET Web Pages. Once you click OK, you'll see a new folder appear in your project called _bin_deployableAssemblies with the required libraries.

For .NET, this is a special folder:

  • This is a secondary bin folder for framework dependencies.  If you right-click on any one of the .dll's shown in this folder, you'll see that the libraries are set to "Copy to Output Directory".  When the application is packaged/deployed, the libraries (and the folder) are also copied.  Again, the framework will also automatically check this folder for dependencies.
  • For .NET, any folder that begins with an underscore ("_") is considered private and non-accessible to the browser.  Therefore, you can rest knowing that the dependencies are secure.

Note that this process did not add any references to these libraries in your project.  They are simply here for the framework to run your MVC application.  If you do, in fact, need a type or class from one of these libraries, you are free to still add it as a reference as you normally would.

The above method is for Visual Studio 2010. How about in Visual Studio 2012? Then, please stay here, don’t exit from our blog. We almost finish our tutorial. Stay focus. :)

After Visual Studio 2010, the "Add Deployable Assemblies" option was removed from the project's options menu.  The system libraries are automatically copied to the Bin folder of your project. However, again, the 'Microsoft.Web.Infrastructure.dll' is only available on machines that have Visual Studio installed.  So how do you deploy this library with you application if you don't want to install Visual Studio on a production environment?  I'm glad you asked.  You'll need to use the Package Manager.

1. Run the following command in the package manager (if you're not familiar with Visual Studio, this can be reached by going to "Tools --> Library Package Manager --> Package Manager Console" in the top menu).

2. At the PM> prompt type

Install-Package Microsoft.Web.Infrastructure

3. You will see then see a message indicating that it has been installed and added to your project.

4. You'll also see that 'Microsoft.Web.Infrastructure.dll' has been added as a reference in your References folder.

5. Finally, now, when you deploy your application, 'Microsoft.Web.Infrastructure.dll' will be included in the Bin folder.

Conclusion

We hope with tutorial above, you can easily deploy your MVC application without any problem. If this article, please share it!! Please tell the world. :)