'Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator"
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 :
Chosen Empty Template with MVC checked :
Tried to Add View
from a Controller :
Some settings ...
The Error :
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 !
Solution 1:[1]
Try clearing the ComponentModelCache, the cache will rebuild next time VS is launched.
- Close Visual Studio
- Delete everything in this folder C:\Users\
[your users name]
\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache - Restart Visual Studio
14.0 is for visual studio 2015. This will work for other versions also.
Solution 2:[2]
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:[3]
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:[4]
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.
Solution 5:[5]
In my case helped the following:
- Restart VS
- Solution Explorer => Right click on solution => Rebuild solution
Solution 6:[6]
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:[7]
I solved this problem in this way
first I had Entity frameworks with the latest version 5.0.5
and the code generation package with version 5.0.2
so I just uninstalled Entity frameworks and install version 5.0.2 as the same for code generation package
and its worked
Solution 8:[8]
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:[9]
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:[10]
C:\Users\{WindowsUser}\AppData\Local\Microsoft\VisualStudio\16.0_8183e7d1\ComponentModelCache
Remove from this folder for VS 2019 ....
Solution 11:[11]
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:[12]
I just restarted my visual studio and it worked.
Solution 13:[13]
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:[14]
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:[15]
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:[16]
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:[17]
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:[18]
A simple VS restart worked for me. I just closed VS and restarted.
Solution 19:[19]
None of these solutions worked for me. I just updated Visual Studio (updates were available) and suddenly it just works.
Solution 20:[20]
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:[21]
Deleting the .vs folder inside the solution directory worked for me.
Solution 22:[22]
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:[23]
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:[24]
Check your Database Connection string and if there any syntax errors in the appsettings.json it will return this error.
Solution 25:[25]
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:[26]
Try unload and reload project (solution).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow