Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have searched extensively and cannot find another person on the planet with this problem...
The symptoms are:
In Visual Studio 2012, on a MVC 4.0 Project that was upgraded from VS2010 and is configured for dual version editing from VS2012 and VS2010. When I right-click on the Controllers Folder and select Add->Controller... from the context menu, I get an "Object reference not set to an instance of an object" error from Visual Studio.

The existing code in the project runs fine, and if I use the Add->New Item option instead, and pick the Controller from the dialog box, then I can add a blank controller. I just cannot add a typed controller using the method above.

If I however create a new MVC4 Project, then I can add typed controllers again using the same menu, so the problem does not exist in Visual Studio's installation, it has to exist in the Project File.
I have also checked the references between my project and a new MVC Project and all the new project references are included in my project.
Has anyone experienced something similar?

How do I fix this?

like image 312
Francois Grobler Avatar asked Apr 22 '13 13:04

Francois Grobler


4 Answers

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.

like image 186
Francois Grobler Avatar answered Oct 17 '22 16:10

Francois Grobler


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.

like image 44
웃mauri Avatar answered Oct 17 '22 14:10

웃mauri


Maybe your existing project have wrong ProjectTypeGuids element in .csproj file? Right one for MVC4 is {E3E379DF-F4C6-4180-9B81-6769533ABE47}.

Try to compare what project type do you have in new project and in your existing project.

like image 36
milunka Avatar answered Oct 17 '22 14:10

milunka


The preferred answer of installing the Windows Management Framework 3.0 did not work. The download "Windows6.1-KB2506143-x86.msu" did not work on my Windows 7 32 bit system. It failed with the error "The update is not applicable to your computer".

Reinstalling MVCscaffolding with Manage Nuget Packages did not work.

My ProjectType GUID was correct.

Microsoft Web Developer Tools was already installed.

Deleting and recreating the solution didn't work.

It was necessary to repair the Visual Studio 2012 installation from the install disk to fix this issue.

like image 1
user1161391 Avatar answered Oct 17 '22 14:10

user1161391