Cannot add a controller [duplicate] - c#

I am working on an ASP.NET MVC project and so far I had no problems with scaffolding any type of items until now.
Every time I want to create a new controller or view, I get the following error message:
There was an error running the selected code generator: 'The value -1 is outside the acceptable range of [0,2147483647]. Parameter name :value'
This issue only arises in the project that I'm currently working on as I tried creating a new controller in another project and it worked flawlessly.
I already tried the solution to delete the ComponentModelCache folder and rebuilding the project but the problem still persists.
My Visual Studio 2019 is also up to date.
EDIT:
Pulling the project from git source control on another PC and restoring the packages fixed the issue for me. You probably don't have to do it from another computer, just try deleting the project and restoring it from remote.
EDIT 2:
Creating a second view and the same problem is back again...

I fixed this by unloading and reloading the project

You just need to - delete ".vs" folder from your solution directory.
In summary only the deleting of the .vs folder is what has worked for me.
On reopening VS2019 16.2.3 - I can now add in Controllers and Views without seeing this error.
Found it from Microsoft Developer Community site: https://developercommunity.visualstudio.com/content/problem/626755/error-running-the-selected-code-generator-value-1.html?childToView=684261#comment-684261
code-generator-solution

This issue has been resolved in Visual Studio 2019 version 16.2.4. The release notes are available here:
https://learn.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.2.4

One of the possibility is if your web.config file is open then this error might appear. Try closing the web.config file and then add a View.
You can also reach out to this website for more reference: Error Running the Selected Code Generate Value
Hope this helps.

I was able to fix this by replacing the web project with a new one.
-I renamed the project like project_old folder and removed it from solution.
-Added a new project to the solution by right click and add project
-Created the old controllers and everything with copying from old project files
-Updated nuget packages and deleted old project files
Clean Solution -> Rebuild Solution and voilla

i fix it, deleting the .vs folder, re opening the project, and clenning the solution

Close all documents, and try again to create new controller.
It works for me in Visual Studio 2019 asp.net MVC but I don't know why

I was able to fix this by manually deleting the BIN and OBJ folders for my projects. Running Clean in Visual Studio did not work.

I was getting this error - as my project is a git repository, I ran git clean -xfd on it, then rebuilt it and was able to add a controller when I tried again.

try both any one will work either of them.
1.Just delete .vs folder in project solution.
2.If above fix does not work.,close your web.config file if it is opened.

I tried it all and finally found a solution that worked, this issue only happens when the web.config file is open in the editor. Hope this help.
Credits :
Vijay Ramakrishnan [MSFT]
https://developercommunity.visualstudio.com/content/problem/626755/error-running-the-selected-code-generator-value-1.html

I fixed this by matching the version of "Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" to the rest.
Replaced everything that was 5.0.9 with 5.0.2 and it worked.

Go Back to VS 2017 and it will work just fine

just use vs15. In many regards vs19 is still beta...

Related

Visual Studio and Project don't see new classes/folders etc after adding new classes and folders for the project cloned from GIT

I cloned my project from the github repository.Now i made a new folder in this project and added here a class with a namespace.However, when i tried to add a using command with this namespace(c#) to another class(this class was in my project from the start, because it was in git repo), I noticed that project doesn't see this Folder/Class/Namespace.I don't really know what can I do so I'll be very grateful if you will help me(VS 2019)
Try to open the project file (unload the project and edit proj file)
Check if the Folder is in there.
If not, you can manually insert it and reload the project.
Oh and in case that you reference to another project, check if the newer projects framework version is not over the referencing project.
This could also be a possible error.
I know it's quite old topic, but I've got the same issue today. And the reason was that I created repo with a name containing space, like "My repo".
In the link generated by git, repo name was "My%20repo". And the same directory was created on my local machine (os: win 10).
It appears that VS can't treat "My%20repo" as valid directory name, and no directory nor file has been added to project on creation. Strange thing is, any file or directory created later has been perfectly visible.
So I've just changed directory name to get rid of this "%20" part, now it works just as supposed.
Not sure if that's true reason. But that's my experience I wanted to share :)
Follow this link, may be it could help:
visual-studio-2019-doesnt-include-on-csproj-new-files
This solve the issue: update the package Microsoft.Net.Compiler to the latest version, compile and try adding a new class to your project again.

"There was an error running the selected code generator: 'The value -1 outside the acceptable range of [0,2147483647]. Parameter name :value''"

I am working on an ASP.NET MVC project and so far I had no problems with scaffolding any type of items until now.
Every time I want to create a new controller or view, I get the following error message:
There was an error running the selected code generator: 'The value -1 is outside the acceptable range of [0,2147483647]. Parameter name :value'
This issue only arises in the project that I'm currently working on as I tried creating a new controller in another project and it worked flawlessly.
I already tried the solution to delete the ComponentModelCache folder and rebuilding the project but the problem still persists.
My Visual Studio 2019 is also up to date.
EDIT:
Pulling the project from git source control on another PC and restoring the packages fixed the issue for me. You probably don't have to do it from another computer, just try deleting the project and restoring it from remote.
EDIT 2:
Creating a second view and the same problem is back again...
I fixed this by unloading and reloading the project
You just need to - delete ".vs" folder from your solution directory.
In summary only the deleting of the .vs folder is what has worked for me.
On reopening VS2019 16.2.3 - I can now add in Controllers and Views without seeing this error.
Found it from Microsoft Developer Community site: https://developercommunity.visualstudio.com/content/problem/626755/error-running-the-selected-code-generator-value-1.html?childToView=684261#comment-684261
code-generator-solution
This issue has been resolved in Visual Studio 2019 version 16.2.4. The release notes are available here:
https://learn.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.2.4
One of the possibility is if your web.config file is open then this error might appear. Try closing the web.config file and then add a View.
You can also reach out to this website for more reference: Error Running the Selected Code Generate Value
Hope this helps.
I was able to fix this by replacing the web project with a new one.
-I renamed the project like project_old folder and removed it from solution.
-Added a new project to the solution by right click and add project
-Created the old controllers and everything with copying from old project files
-Updated nuget packages and deleted old project files
Clean Solution -> Rebuild Solution and voilla
i fix it, deleting the .vs folder, re opening the project, and clenning the solution
Close all documents, and try again to create new controller.
It works for me in Visual Studio 2019 asp.net MVC but I don't know why
I was able to fix this by manually deleting the BIN and OBJ folders for my projects. Running Clean in Visual Studio did not work.
I was getting this error - as my project is a git repository, I ran git clean -xfd on it, then rebuilt it and was able to add a controller when I tried again.
try both any one will work either of them.
1.Just delete .vs folder in project solution.
2.If above fix does not work.,close your web.config file if it is opened.
I tried it all and finally found a solution that worked, this issue only happens when the web.config file is open in the editor. Hope this help.
Credits :
Vijay Ramakrishnan [MSFT]
https://developercommunity.visualstudio.com/content/problem/626755/error-running-the-selected-code-generator-value-1.html
I fixed this by matching the version of "Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" to the rest.
Replaced everything that was 5.0.9 with 5.0.2 and it worked.
Go Back to VS 2017 and it will work just fine
just use vs15. In many regards vs19 is still beta...

ASPNETCOMPILER(0,0): Error ASPRUNTIME: Object reference not set to an instance of an object

I'm Trying to publish a website, using Visual Studio 2013.
I'm getting an error:
ASPNETCOMPILER(0,0): Error ASPRUNTIME: Object reference not set to an instance of an object.
It happens also on Visual Studio 2010.
I tried to restart VS, and PC.
I removed *.dll.refresh from my bin directory.
I don't have <clear/> tags in my web.config.
I think it all started when I accidently removed my *.suo file.
Any ideas?
Update:
I just noticed it happens only when I try to precompile my site. If I publish without precompilation it works fine...
I also had this problem and it was not solved by any of the ideas mentioned previously. The problem was that somehow compresssion had been enabled on the subfolder of my drive containing the ASP.NET website. The solution is to right-click, choose properties and in the general tab, click Advanced and under the 'Compress or Encrypt attributes' section, make sure 'Compress contents to save disk space' is unchecked. Upon prompting do this for all files and subfolders (note it may take some time). Hope this helps someone.
OK.
After wasting so many hours I somehow got to this blog:
http://thesoftwarepractice.net/development-things/object-reference-not-set-to-an-instance-of-an-object
Turned off the McAfee Real Time Scanning and its works...
This is a hard to catch issues, which usually comes with webforms aspx pages.
If you are deploying the app with pre-compiled option and you have selected the "Do not merge, create a separate assembly for each page and control" here:
then you need to make sure that there are no compiled assemblies for any of the aspx pages in the bin folder of the project you are trying to compile. Go to your bin folder and delete any assemblies related to your web pages and try publishing again. This will fix this issue.
Have you tried creating a new website, adding all the files from old website to the new one and adding all the references used?
I also had this issue, and I fixed it by clearing and re-configuring all the dependencies and the dll's of the project.
I have deleted files from bin folder and then build solution and publish application.
It works for me.
In my case was only failing from commandline (msbuild), and from CI agents. The solution I've found is to disable the check "Allow precompiled site to be updatable in Project->Page Properties->MsBuild

Visual Studio displaying errors even if projects build

I have a problem with Visual Studio on a C# solution. It displays totally random errors, but the projects build. Right now, I have 33 files with errors, and I can see red squiggly lines in all of them.
I tried cleaning / rebuilding the solution, closing Visual Studio and even restarting my computer. I can modify .cs files and I see the changes in the solution.
Does anyone have an idea about why it does that?
If you have ReSharper, try emptying the ReSharper cache:
In menu, ReSharper > Options > Environment > General > Clear Caches
and disabling and re-enabling ReSharper:
In menu, Tools > Options > ReSharper > General > Suspend / Restore
Clearing Resharper's cache did not help in my case, tried suspend/restore, and also Repair Resharper, using latest download off JetBrains' website - neither of these helped. This is after I tried close/reopen VS, restart my machine, repeat, Build/Rebuild and combination thereof.
It's interesting that suspending Resharper seemed to solve the problem after the 2nd restart of VS, but it was back after I enabled Resharper <-- I tried to do this sequence 2-3 times to ensure the pattern.
Anyway, I was still having issues when I found this article:
Quick tip: What to do when Visual Studio freaks out and everything is red
So I deleted the hidden .SUO file on the same folder level with solution, and it magically solved all reds.
Note - for Visual Studio 2015, the .SUO file is in .vs/[solution_name]/v14 hidden folder.
tldr; Unload and reload the problem project.
When this happens to me I (used to) try closing VS and reopen it. That probably worked about half of the time. When it didn't work I would close the solution, delete the .suo file (or the entire .vs folder) and re-open the solution. So far this has always worked for me (more than 10 times in the last 6 months), but it is slightly tedious because some things get reset such as your build mode, startup project, etc.
Since it's usually just one project that's having the problem, I just tried unloading that project and reloading it, and this worked. My sample size is only 1 but it's much faster than the other two options so perhaps worth the attempt. (Update: some of my co-workers have now tried this too, and so far it's worked every time.) I suspect this works because it writes to the .suo file, and perhaps fixes the corrupted part of it that was causing the issue to begin with.
Note: this appears to work for VS 2022, 2019, 2017, and 2015.
I cleaned solution, closed VS, reopened it, build solution, and red unresolved lines were cleaned and build succeeded.
I found that happens frequently when using Git in Visual Studio 2017, switching branches where there is dependent code changes. Even though the project will build successfully, there will remain errors in the error list.
These errors are often namespace issues and missing references, even when the library reference exists.
To resolve:
Close Visual Studio
Delete the {sln-root}.vs\SlnName\v15.suo file (hidden)
Restart Visual Studio
I have tried all the 6 options, nothing worked for me. Below solution resolved my issue.
Close VS.
Delete the hidden ".vs" folder next to your solution file.
Restart VS and load the solution.
Here's a collection of popular answers. Upvote the OP of the answer if it helped you:
Option 1: Clean, Build and Refresh (#Mike Fuchs option)
As #Mike Fuchs mentioned, try the following operations:
In menu, Build > Clean Solution
And
In menu, Build > Build Solution
and select the project in question, and click on the refresh button:
Option 2: Clean, Close, Restart and Build (#Pixel option)
As #Pixel mentioned, try the following sequence of operations:
Clean the solution
Close Visual Studio
Open Visual Studio
Build solution
Option 3: Clear ReSharper cache (#GammaOmega option)
If you have ReSharper, try emptying the ReSharper cache:
In menu, ReSharper > Options > Environment > General > Clear Caches
and disabling and re-enabling ReSharper:
In menu, Tools > Options > ReSharper > General > Suspend / Restore
Option 4: Delete the .suo file (#Neolisk option)
As #Neolisk mentioned, deleting the .suo file might solve your problem. For Visual Studio 2015, the file is located in:
[Path of Solution]/.vs/[Solution Name]/v14/.suo
And for Visual Studio 2017:
[Path of Solution]/.vs/[Solution Name]/v15/.suo
Note that the .vs directory is hidden.
Option 5: Unload and Reload Project (#TTT option)
As #TTT mentioned, try unloading the project that causes problems:
In Solution Explorer, right-click on project, Unload Project.
And re-loading it
In Solution Explorer, right-click on project, Reload Project.
Option 6: Remove and add Microsoft.CSharp reference (#Guilherme option)
As #Guilherme mentioned, try removing and adding the reference to "Microsoft.CSharp" from the projects that have problems.
In Solution Explorer, expand the project, expand "References", right-click on "Microsoft.CSharp" and Remove.
Then, right-click on References > Add Reference, select "Microsoft.CSharp" from the list and click OK
Delete the hidden file path = your solution\ .vs\ your solution Name \v15\ .suo
I had a problem like this where Intellisense didn't seem to recognise the existence of one project (lots of "can't find this type", "this namespace doesn't exist", etc. errors).
Removing and re-adding the project reference in all the referencing projects would fix the issue, but the underlying cause could be fixed by editing the .proj file of the problem project.
Near the top of the "missing" project' .csproj file is an element:
<ProjectGuid>{GUID}</ProjectGuid>
and in all of the referencing projects .csproj files were project references:
<ProjectReference Include="..\OffendingProject\OffendingProject.csproj">
<Project>{ANOTHER-GUID}</Project>
<Name>Offending Project</Name>
</ProjectReference>
The referencing GUID didn't match the project's GUID. Replacing {GUID} above with {ANOTHER-GUID} fixed the problem without having to go through every referencing project.
So many things that could cause it, as evidenced by the long list of answers here. Here's what fixed it for me, having tried pretty much everything else first.
Build your sulution in DEBUG mode. Then build it in RELEASE mode (it shouldn't build when it has red wavy lines, but in my case it was just warnings that should have had green wavy lines but it was getting in a muddle and giving them red wavy lines, and it built anyway even in release mode). Then build in in DEBUG mode. Spitting on your hands and turning around three times optional.
Worked for me, when nothing else did.
for VS-2017, deleting .vs folder worked for me.
Following solution worked for me
1 - Close VS
2 - Delete .vs folder
3 - Open VS
4 - Build solution
I've noticed that sometimes when switching git branches, Visual Studio (2017) will not recognize types from some files that had been added in the second branch. Deleting the .vs folder solves it, but it also trashes all your workspace settings. This trick seems to work well for me:
Solution Explorer -> Find the file with the unrecognized class in it.
Click Show All Files at the top of the Solution Explorer.
Right-click the file -> Exclude from project.
Right-click the file again -> Include in project.
This causes Intellisense to parse the file that it missed when switching branches.
Occasionally I have to do a custom clean by going through all of the projects and manually deleting the "bin" and "obj" folders. To see them in Visual Studio, you'll have to enable hidden files and folders for each project. After this is done, rebuild the solution.
My symptoms in VS2019 were that I would build with some errors. Then I'd fix the errors, and the build would work, as shown in the Output window. But the Errors windows still showed the old errors. I could run it just fine. Closing VS2019 and reopneing fixed the issue, but only for a little while. This started happening on version 16.4.3
This solution seems to work for me:
Uncheck Tools->Option->Projects and Solutions->General->Allow parallel project initialization
I found this fix buried way down in the comments here: https://developercommunity.visualstudio.com/content/problem/483450/vs-2019-intellisense-reports-compile-errors-when-r.html
Perhaps you try to reset your intellisense cache. I've had a similar issue in visual studio 2012 when working in a large project with many partial class definitions.
Reducing the partials solved the problem partially, clearing the intellisense cache also - for a while.
Deleting .vs folder solved my problem.
But it also reset my solution's current settings in VS. Like, my unloaded projects in the solution were re-loaded and all the pinned and opened documents were also closed when I restarted the VS.
0 - Right click on the Solution and clean solution
1 - Close VS
2 - Delete project's .suo file
3 - Open VS
4 - Build solution
A colleague of mine experienced this issue today. We tried many of the recommendations here and none worked except the solution described below.
Problem:
Project builds fine but Intellisense fails to recognize certain types and marks particular using statements as invalid.
Solution:
Change the 'Solutions Platform' (in VS 2017 this is the dropdown next to the Solution Configuration dropdown and has values such as x86, x64, AnyCPU, Mixed Platforms, etc.) to AnyCPU.
The platform for your project may vary, but it seems as though some references may not be valid for all platforms.
In my case helped a combination of things:
deleting all old not needed files which were previously excluded from the project
closing VS
deleting all the bin folder contents
deleting .vs folder
Clean/Rebuild
after that I still had some spurious errors, however the amount was significantly lower (from 200 to around 8) and the errors referred only to a resource dictionary path in Generic.xaml e.g. <ResourceDicitonary Source="example/path/somefile.xaml"> when I played around with the path trying to change it to a wrong one re-building then correcting it and rebuilding again, then this finally cleared all errors. It was specifically WPF project if that's relevant.
For my specific case it was a service reference another developer merged into the main branch. Which was perfectly fine except syntax highlight failed to resolve the generated service class and source was all red underlined. Cleaning, rebuilding, restarting did nothing.
All I had to do was refresh the service reference and VS managed to put the pieces together behind the scenes. No changes in the source code or generated files.
I've just ran into this issue after reverting a git commit that added files back into my project.
Cleaning and rebuilding the project didn't work, even if I closed VS inbetween each step.
What eventually worked, was renaming the file to something else and changing it back again. :facepalm:
After trying all of the options listed I discovered yet another reason why this can happen. If somebody sent you the source code as a zip, or you downloaded a zip, Windows may have blocked all files. 2 ways to solve this:
Method 1:
Right click on the original Zip file -> Check 'Unblock' -> Click apply
Method 2:
If that's not an option, rather than opening properties on every file in the solution folder simply open power shell and unblock recursively using the following:
Get-ChildItem -Path 'C:\<ROOT FOLDER OF SOLUTION>\' -Recurse | Unblock-File
first close the solution.
then solution cache file delete( in location C:\Users\Documents\Visual Studio\Backup Files/project cache file)
then .suo file delete
then solution open and build.
I hope solve your problem
Had this issue at work (running VS2017). Tried all of the answers here. No joy.
The project would build just fine, but was complaining that namespaces / types couldn't be found. Red squiggles all over the place. Lots of errors in the Error List window.
My solution contained 3 projects.
Discovered that 3 of the NuGet library references for one of the projects was out of line.
Consolidated the referenced library versions, and Bingo.
Hope this helps someone.
Brett.
Unload & reload the project fixed this problem.
I've been struggling with this issue for over a year and none of these solutions helped me:
Delete .suo
Delete .vs folder
Delete any or all cache / temp folders
Delete obj / bin folders
Unload / reload project
I finally fixed this issue - I opened the vbproj/csproj file in notepad and noticed that in the ItemGroup section, there was a reference back to my main project dll. I deleted this reference, reopened my solution and the problem was fixed.
Sometimes if you just clean solution the errors are disappearing, but they may eventualy come back afer a while or at the next build.
Ran into this issue with a single type not being recognized by Visual Studio which showed the red squiggle even though the solution built successfully. I noticed in the Solution Explorer the file did not have the expand arrow on the left which shows classes and properties on expansion.
The fix was to Exclude the file from the project and save/build which produced an expected error and then Include the file in the project and save and build.
After performing these steps Visual Studio started to recognize my type again. Looking at the diff in git it appears the issue was due to line endings not matching on the <Compile Include="..." /> line of my .csproj file.
in my case vs was never retaining the imported namespaces in the project properties > references
when I tried to add/check them again I couldn't and vs threw an error and when saved project vs crashed. When I reopened all the standard imported namespaces (system.data etc...) were all ticked again and it then was recognising everything without error

ClickOnce - Cannot publish because a project failed to build

When I try to publish my (WPF, C#) application, I get these errors:
Cannot publish because a project failed to build.
Could not find file 'obj\x86\Debug\MyAPP.exe'
I get these errors no matter where from I publish: publish wizard, build menu or right click on project - publish.
First it was working ok, but I did the following:
I changed the date om my computer to 10/10/2013, I was trying something else. I forgot this and I click build. Then I set the proper date on computer. After that I get these errors.
Also every time I click run Visual Studio builds project whether there are changes in project.
I also noticed that when I set the date after 10/10/2013 it works ok.
I am guessing that I am looking for some settings in my project where this date of build is set.
I tried build, rebuild, clean solution.
This is a problem with Visual Studio that can occur when you have add-ins installed. Instead of using the Publish button in the Publish tab, use Build/Publish from the menu on the top of Visual Studio.
If you use the Publish button, it runs through the add-ins before doing the build (or something like that). If you use Build from the menu instead, it goes straight to msbuild and ignores any add-ins you have installed.
If you have DevExpress installed and are still having problems, check out this article:
http://www.devexpress.com/Support/Center/p/Q260132.aspx
With the date back to normal, close VS and try deleting your *.suo files next to the *.sln files and then reopen the solution
I'm not sure exactly how your development machine can get fouled up this way, but this started happening for several developers in our group too.
After researching it, it appears that the built in build/publish script that Visual Studio (2010 in our case) uses has a flaw in the order that it does things. Most importantly it runs a cleanup on the OBJ directory deleting the target EXE file before the publish step can grab it.
The solution
This is somewhat of a hacky workaround, but it solved the problem for me.
The fix is to copy the file back to the /obj/ folder from the /bin/ folder right before the publish step. Unfortunately there is no way that I know to specify a BeforePublish event through the IDE, so you will have to edit the .vbproj file in a text editor.
Add the following section just before the final </project> tag.
<Target Name="BeforePublish">
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(IntermediateOutputPath)" />
</Target>
I solved my problem. The problem was the "Modified" time of some files. I used the program Timestamp to fix it.
As I said in my question, the problem was that I build the solution and the date was in the future. Because of that, every time I pressed F5 Visual Studio was building the whole solution again and Publish ClickOnce wasn't working.
If nothing else works, try this :
Set your date to the proper date.
With VS closed, delete all the bin and obj folders of your solution.
Create a new empty solution.
Add your existing projects to the solution.
Add back the needed references.
Rebuild to make sure you didn't miss anything.
Publish.
If you want, you can also just try step 1, 2, 6, 7 before, if you don't feel like making a new solution.
Just create a new ClickOnce manifest certificate, and it will work again.
I have sometimes had problems with an ASP.NET project publishing, because the publish wizard couldn't delete files in the directory it was trying to publish the files to.
Try clearing out those file manually before publishing. Also check the output window; it will sometimes give you some hints as to why the publish failed.
I just ran into the problem today. In my particular case, it was caused by Microsoft Windows Update, Microsoft Security Advisory 2661254 (concerning minimum certificate key length).
I uninstalled that update and publish began working as it always had. This is only a temporary work around and means you should probably regenerate your keys/certificates.
I've had this error when I'd been swapping git branches. A Rebuild All fixed this for me.
In my case, the problem arise when I added the following section in .csproj File:
<Target Name="AfterBuild">
</Target>
Closing and Opening Visual Studio worked for me.

Categories