Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator"

41

I'm following a video tutorial where I'm required to create an empty ASP.NET Web Application with MVC, using Visual Studio 2015, being new to ASP.NET world, I'm following step by step.

I got my project created well, next step adding a View from an existing Controller, I got hit by a messagebox error saying :

Error :
There was an error running the selected code generator: 'Invalid pointer (Exception from HRESULT:0x80004003(E_POINTER))'

I Googled the problem, found similar issues, but none leaded to a clear solution, some similar problems where issued by anterior version of VisualStudio, but as I said none with a clear solution.

To clarify what I experienced, here's what I've done step by step :

Chosen a ASP.NET Web Application :

enter image description here

Chosen Empty Template with MVC checked :

enter image description here

Tried to Add View from a Controller :

enter image description here

Some settings ...

enter image description here

The Error :

enter image description here

What's causing this problem and What is the solution for it ?

Update :

It turns out that even by trying to add the View manually I get the same error, adding a view is all ways impossible !

asp.net-mvc
visual-studio
visual-studio-2015
asked on Stack Overflow Jan 25, 2016 by AymenDaoudi • edited Apr 3, 2016 by tereško

25 Answers

78

Try clearing the ComponentModelCache, the cache will rebuild next time VS is launched.

  1. Close Visual Studio
  2. Delete everything in this folder C:\Users\ [your users name] \AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
  3. Restart Visual Studio

14.0 is for visual studio 2015. This will work for other versions also.

answered on Stack Overflow Mar 5, 2016 by longday • edited Mar 4, 2020 by VSB
14

I had this issue with a different error message "-1 is outs the bounds of.."

The only thing that worked for me, was to remove the project from the solution by right clicking the project and selecting 'Remove'. Then right click the solution, Add Existing Project, and selecting the project to reload it into the solution.

After reloading the project, I can now add views again.

answered on Stack Overflow Aug 29, 2019 by lucky.expert
6

I have the same error but in VS 2017 when I create a controller. I did it in the same way as @sh.alawneh wrote. Also, I tried to do what @longday wrote. But It didn't work. Then I tried in another way:

  • Right click on the target folder.
  • From the list, choose Add => New Item.

There I choose a new controller and it works fine.

Maybe I'll help someone.

answered on Stack Overflow Dec 10, 2018 by Maksym Labutin • edited Dec 23, 2019 by Maksym Labutin
5

Follow these steps to add a view in a different way than the typical way:

  • 1) Open Visual studio.
  • 2) Create/open your project.
  • 3) Go to Solution Explorer.
  • 4) Right click on the target folder.
  • 5) From the list, choose Add.
  • 6) From the child list, choose MVC View Page (Razor) or MVC View Page with layout (Razor).
  • 7) If you select the second choice from the previous step, you should choose a layout page for your view from the pop up window.
  • 8) That's it!

If you cannot open the view that you are created, simply right click on the view file, choose Open with, and select HTML (web forms) editor then okay.

answered on Stack Overflow Jul 27, 2016 by sh.alawneh • edited Jul 27, 2016 by dorukayhan
2

Right-click on the project under the solution and click unload Project,

you will see that the project is unloaded, so now re right-click on it and press load project

then try to add your controller

answered on Stack Overflow Nov 19, 2019 by MOHAMMAD OMAR
2

In my case helped the following:

  1. Restart VS
  2. Solution Explorer => Right click on solution => Rebuild solution
answered on Stack Overflow Nov 29, 2020 by essential
1

Lets assume you are using a datacontext in a DLL, well, it was my case, and I dont know why I have the same error that you describe, I solve it creating a datacontextLocal on the backend project. then I pass the Dbset to the correct datacontext and delete the local (you can let it there if you want, can be helpfull in the future

if you ready are using it in local datacontext, create a new one and then pass the Dbset to the correct one

answered on Stack Overflow Sep 29, 2017 by sGermosen
1

In ASP.NET Core check if you have the Microsoft.VisualStudio.Web.CodeGeneration.Tools nuget package and it corresponds to your project version.

answered on Stack Overflow Aug 21, 2018 by Oleksandr Kyselov
0

just in case someone is interested - the solution with clean cache didnt work for me. but i've managed to solve an issue but uninstalling all .Net frameworks in the system and then install them back one by one.

answered on Stack Overflow Jun 15, 2017 by Leonid
0

I just restarted my visual studio and it worked.

answered on Stack Overflow Mar 21, 2018 by Kurkula
0

Try clearing the ComponentModelCache,

1.Close Visual Studio

2.Delete everything in this folder C:\Users\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache

3.Restart Visual Studio

4.Check again

this also used VS2017 to get solution

answered on Stack Overflow May 28, 2018 by Deepak Savani
0

I ran into a similar issue that prevented the code generation from working. Apparently, my metadata had unknown properties or values. I must admit I did not try all the answers here but who really wants to reinstall vs or download any of the numerous Nuget packages being used.

Cleaning the project worked for me (Build->Clean Solution) The generator was using some outdated binaries to build the controller and views. Cleaning the solution removed the outdated binaries and voilà.

answered on Stack Overflow Aug 28, 2018 by user3416682
0

I'm currently trying to familiarise myself with MVC 4. I've been developing with MVC 5 for a while, but I need to know MVC 4 to study for my MCSD certification. I'm following a tutorial via Pluralsight, targeting much older versions of Entity Framework, and MVC, (the video was released in 2013!)

I hit this exact same error 2 hours ago and have been tearing my hair out trying to figure out what is wrong. Thankfully, because the project in this tutorial is meaningless, I was able to step backward throughout the process to figure out what it was that was causing the object ref not set error, and fix it.

What I found was an error within the structure of my actual solution.

I had a MVC web project set up ('ASP.NET Web Application (.NET Framework)'), but I also had 2 class libraries, one holding my Data Access Layer, and one holding the domain setup for models connecting to my database.

These were both of type 'Class Library (.NET Standard)'.

The MVC project did not like this.

Once I created new projects of type 'Class Library (.NET Framework)', and copied all the files from the old libraries to the new ones and fixed the reference for the MVC web project, a rebuild and retry allowed me to scaffold the View correctly.

This may seem like an obvious fix, don't put a .NET Standard project alongside a .NET Framework one and expect it to work normally, but it may help others fix this issue!

answered on Stack Overflow Aug 31, 2018 by IfElseTryCatch
0

My problem was the Types used in the Model Class.

Using the Types like this:

    [NotMapped]
    [Display(Name = "Image")]
    public HttpPostedFileBase ImageUpload { get; set; }


    [NotMapped]
    [Display(Name = "Valid from")]
    public Nullable<DateTime> Valid { get; set; }


    [NotMapped]
    [Display(Name = "Expires")]
    public Nullable<DateTime> Expires { get; set; }

No longer works in the Code Generator. I had to remove these types and scaffold without them, then add them later.

It is silly, [NotMapped] used to work when scaffolding.

Use the base Types: int, string, byte, and so on without Types like: DateTime, List, HttpPostedFileBase and so on.

answered on Stack Overflow Mar 19, 2019 by Rusty Nail
0

I have been scratching my head with this one too, what i found in my instance was that an additional section had been added to my Web.config file. Commenting this out and rebuilding solved the issue and i was now able to add a new controller.

answered on Stack Overflow May 18, 2019 by Icementhols
0

C:\Users\{WindowsUser}\AppData\Local\Microsoft\VisualStudio\16.0_8183e7d1\ComponentModelCache

Remove from this folder for VS 2019 ....

answered on Stack Overflow Aug 30, 2019 by Farzad Sepehr
0

A simple VS restart worked for me. I just closed VS and restarted.

answered on Stack Overflow Sep 19, 2019 by Sajithd
0

None of these solutions worked for me. I just updated Visual Studio (updates were available) and suddenly it just works.

answered on Stack Overflow Oct 16, 2019 by Exel Gamboa
0

The issue has been resolved after installed EntityFramework from nuget package manager into my project. Please take a look on your project references which already been added EntityFramework. Finally I can add the view with template after I added EntityFramework to project reference.

answered on Stack Overflow Oct 23, 2019 by Ei Ei Phyu
0

Deleting the .vs folder inside the solution directory worked for me.

answered on Stack Overflow Dec 16, 2019 by Adam Hey
0

I am working on a Core 3 app and had this issue pop up when adding a controller. I figured out that the Microsoft.VisualStudio.Web.CodeGeneration.Design package was updated with a .net 4.x framework dll. Updating to the project to Core 3.1 fixed the issue.

answered on Stack Overflow Dec 18, 2019 by Hoodlum
0

I know this is a really old thread, but I came across it whilst working on my issue. Mine was caused because I had renamed one of my Model classes. Even though the app built and ran fine, when I tried to add a new controller I got the same error. For me, I just deleted the class I had renamed, added it back in and all was fine.

answered on Stack Overflow Jul 27, 2020 by SkinnyPete63
0

Check your Database Connection string and if there any syntax errors in the appsettings.json it will return this error.

answered on Stack Overflow Apr 23, 2021 by Theepag
0

I solved this problem in this way

first I had Entity frameworks with the latest version 5.0.5

enter image description here

and the code generation package with version 5.0.2

enter image description here

so I just uninstalled Entity frameworks and install version 5.0.2 as the same for code generation package

and its worked

answered on Stack Overflow May 3, 2021 by AhmedNour
0

Another common cause for this, which is hinted in the build log, is your IIS Express Web Server is running while you are trying to build a scaffold. Stop/Exit your web server and try building the scaffold again.

answered on Stack Overflow May 23, 2021 by Dean P

User contributions licensed under CC BY-SA 3.0