Cannot add a MVC controller VS2012 gives "object reference not set to an instance of an object"

The issue seems to have been caused by an incomplete install/registration of the MvcScaffolder package. The following process resolved my issue for me.

Start Visual Studio and open the project. Open the Package Manager Console. (This was the "trick" or missing piece of information that led to the solution...)

Here I saw the following error caused by the PowerShell script used for registration:

New-Object : Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
At <ProjectPath...>\packages\MvcScaffolding.1.0.9\tools\registerWithMvcTooling.ps1:163 char:27
+     $newProvider = New-Object <<<<  $powerShellScaffolderProviderType($mvcScaffoldingProvider)
    + CategoryInfo          : NotSpecified: (:) [New-Object], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException, Microsoft.PowerShell.Commands.NewObjectCommand

When I checked the registered scaffolders from the Package Manager Console I got:

PM> get-defaultscaffolder

DefaultName          ScaffolderName
Action               MvcScaffolding.Action
Controller           MvcScaffolding.Controller
CustomScaffolder     T4Scaffolding.CustomScaffolder
CustomTemplate       T4Scaffolding.CustomTemplate
DbContext            T4Scaffolding.EFDbContext
Mailer               Mailer.Razor
Repository           T4Scaffolding.EFRepository
UnitTest             MvcScaffolding.ActionUnitTest
View                 MvcScaffolding.RazorView
Views                MvcScaffolding.Views 

So clearly the scaffolders are configured to use the MvcScaffolding package, but the package had a registration error.

A quick search for the error provided this link http://mvcscaffolding.codeplex.com/discussions/437692 which refers to https://visioautomation.codeplex.com/discussions/397034 that suggests installing PowerShell 3, which is actually part of the Windows Management Framework 3.0, available from http://www.microsoft.com/en-us/download/details.aspx?id=34595. I picked the Windows6.1-KB2506143-x64.msu file when prompted because I’m running Win 7-64-bit.

Restart the PC after installation, re-open Visual Studio and the project and look at the Package Manager Console. If the error is gone, then so is the controller problem.


I had this problem on VS2012 on Win8, We where working on a project that we started developing on VS2010 and i wasn't able to create new controllers with the CTRL+M, CTRL+C shortcut.

Opening any project and re-installing MvcScaffolder and its dependencies under Manage Nuget package have fixed this for me.

Didn't have to restart VS2012 at all.