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

asp.net MvcVisual StudioVisual Studio-2015

asp.net Mvc Problem Overview


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 led to a clear solution, some similar problems were 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 Solutions


Solution 1 - asp.net Mvc

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.

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

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.

Solution 4 - asp.net Mvc

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

    1. Open Visual studio.
    1. Create/open your project.
    1. Go to Solution Explorer.
    1. Right click on the target folder.
    1. From the list, choose Add.
    1. From the child list, choose MVC View Page (Razor) or MVC View Page with layout (Razor).
    1. If you select the second choice from the previous step, you should choose a layout page for your view from the pop up window.
    1. 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.

Solution 5 - asp.net Mvc

In my case helped the following:

  1. Restart VS
  2. Solution Explorer => Right click on solution => Rebuild solution

Solution 6 - asp.net Mvc

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

Solution 7 - asp.net Mvc

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

Solution 8 - asp.net Mvc

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

Solution 9 - asp.net Mvc

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

Solution 10 - asp.net Mvc

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

Remove from this folder for VS 2019 ....

Solution 11 - asp.net Mvc

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.

Solution 12 - asp.net Mvc

I just restarted my visual studio and it worked.

Solution 13 - asp.net Mvc

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

Solution 14 - asp.net Mvc

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à.

Solution 15 - asp.net Mvc

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!

Solution 16 - asp.net Mvc

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.

Solution 17 - asp.net Mvc

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.

Solution 18 - asp.net Mvc

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

Solution 19 - asp.net Mvc

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

Solution 20 - asp.net Mvc

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.

Solution 21 - asp.net Mvc

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

Solution 22 - asp.net Mvc

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.

Solution 23 - asp.net Mvc

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.

Solution 24 - asp.net Mvc

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

Solution 25 - asp.net Mvc

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.

Solution 26 - asp.net Mvc

Try unload and reload project (solution).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAymenDaoudiView Question on Stackoverflow
Solution 1 - asp.net MvclongdayView Answer on Stackoverflow
Solution 2 - asp.net Mvclucky.expertView Answer on Stackoverflow
Solution 3 - asp.net MvcMaksym LabutinView Answer on Stackoverflow
Solution 4 - asp.net Mvcsh.alawnehView Answer on Stackoverflow
Solution 5 - asp.net MvcessentialView Answer on Stackoverflow
Solution 6 - asp.net MvcMOHAMMAD OMARView Answer on Stackoverflow
Solution 7 - asp.net MvcAhmedNourView Answer on Stackoverflow
Solution 8 - asp.net MvcsGermosenView Answer on Stackoverflow
Solution 9 - asp.net MvcOleksandr KyselovView Answer on Stackoverflow
Solution 10 - asp.net MvcFarzad SepehrView Answer on Stackoverflow
Solution 11 - asp.net MvcLeonidView Answer on Stackoverflow
Solution 12 - asp.net MvcKurkulaView Answer on Stackoverflow
Solution 13 - asp.net MvcDeepak SavaniView Answer on Stackoverflow
Solution 14 - asp.net Mvcuser3416682View Answer on Stackoverflow
Solution 15 - asp.net MvcIfElseTryCatchView Answer on Stackoverflow
Solution 16 - asp.net MvcRusty NailView Answer on Stackoverflow
Solution 17 - asp.net MvcIcementholsView Answer on Stackoverflow
Solution 18 - asp.net MvcSajithdView Answer on Stackoverflow
Solution 19 - asp.net MvcExel GamboaView Answer on Stackoverflow
Solution 20 - asp.net MvcEi Ei PhyuView Answer on Stackoverflow
Solution 21 - asp.net MvcAdam HeyView Answer on Stackoverflow
Solution 22 - asp.net MvcHoodlumView Answer on Stackoverflow
Solution 23 - asp.net MvcSkinnyPete63View Answer on Stackoverflow
Solution 24 - asp.net MvcTheepagView Answer on Stackoverflow
Solution 25 - asp.net MvcDean PView Answer on Stackoverflow
Solution 26 - asp.net MvcDee NixView Answer on Stackoverflow