225

I'm using MVC 5 and .net 4.6 to create a web API that my mobile app can connect too.

Whenever I build the project I get hundreds of errors telling me that I need to add references that are already there and that literally every type in my project 'does not exist'. Some examples of these errors: (Not including the one in the title)

ErrorList

What I've tried:

  • Added a reference to System.Runtime via nuGet. (A recommendation from this question.)
  • Removed aspnetcore50 from the frameworks in the project.json file. (Another recommendation, this question.)
  • Restarting VS2015, my machine.
  • Going through the nuGet package manager and upgrading every package in my project to the latest.
  • Actually making sure I can see the references in the references node of the solution explorer.

UPDATE: -My project.json file looks like this. (After modifying based on Karls answer. The error list is now down to ten, however they are still the same errors as before.)

{
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
  },
  "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-beta5",
    "System.Runtime": "4.0.20-beta-23019"
  },
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.Data": "4.0.0.0",
        "System.Data.DataSetExtensions": "4.0.0.0",
        "System.Data.Linq": "4.0.0.0"
      }
    }
  },
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "version": "1.0.0-*",
  "webroot": "wwwroot"
}
2
  • You can remove both aspnet50 and aspnetcore50, they were renamed to dnx/dnxcore in beta3 or something and are useless now (unless they are somehow interfering with the others, if so they are both useless and harmful). Commented Jul 4, 2015 at 19:24
  • Removing those two reduced the error list to 450, getting better!
    – KidCode
    Commented Jul 4, 2015 at 19:25

22 Answers 22

291

Remove bin and obj and rebuild.

6
  • 20
    Thank you that worked, I also needed to unload and reload the projects. Commented Mar 27, 2019 at 9:02
  • 1
    Life savior. I was having this issue inside a docker container Commented Oct 25, 2021 at 21:15
  • 6
    Additionally, I had to clean and build the solution afterwards.
    – alamoot
    Commented Dec 10, 2021 at 17:08
  • 2
    Convenient script to remove obj and bin folders... ;) stackoverflow.com/a/755433/1804678
    – Jess
    Commented Oct 5, 2022 at 12:45
  • Just deleting the OBJ folder worked for me. I had no issue deleting the BIN folder as well but I got a message saying it couldn't be deleted as I was actively running the project. I was too lazy to shut down and restart Visual Studio so I was just like ... whatever I will live with the red squigglies. To my surprise they disappeared. I can now code red squiggly free :D
    – c0d3p03t
    Commented Oct 19, 2022 at 17:02
252

This wasted lot of my time. Close the visual studio. Delete the .vs folder and restart visual studio. Everything should work fine.

Usually this happens when you change branches or merge branches.

12
  • 2
    I had stuff in my applicationhost.config file I didn't want to lose so all I did was close VS, delete the .suo file, and restart VS, and that did the trick for me too. Commented Jan 20, 2021 at 20:27
  • 9
    A similar solution fixed the problem for me - I have deleted the DesignTimeBuild directory (symbols cache) from the .vs directory (deleting the .suo file would have lost solution-level settings, breakpoints, and the like). For sure, I have also deleted the bin and obj directories. The error occurred after manually converting a project to new-style csproj and removing a solution configuration. See also stackoverflow.com/questions/50343456/… . Commented Jun 22, 2021 at 14:13
  • 2
    please mention what changes might occur when that folder is deleted
    – NeWi-SL
    Commented Nov 24, 2021 at 2:38
  • 4
    I hit this after July 2022 patch Tuesday, which included a .net update. Just restarting VS didn't help, but renaming the .vs folder and restarting VS did the trick.
    – Moby Duck
    Commented Jul 13, 2022 at 9:53
  • 2
    You can safely delete the .VS folder. I see a lot of people are renaming the .vs folder or making a backup of it etc. It is safe to delete without making a backup. It's also not something that you should ever check in to source control. it is just something visual studio use to track open files etc. and will regenerate automatically if it does not exist anymore. if you use something like jetbrains rider instead of visual studio, this folder will not exist. Commented Sep 20, 2022 at 19:45
68

I ran dotnet restore command on the Package manager console and the problem was solved.

1
  • Despite the problem was with missing packages that command did not help. Actually, I've noticed that something was blocking PackageManager from resolving the nuget.org source and it was trying to build without required frameworks (in my case Microsoft.Net.Test.Sdk and Microsoft.NETCore.UniversalWindowsPlatform). To resolve that I had to check for a proxy being disabled as well as any VPN connection and recreated the Test project from scratch, so only then it automatically installed all required packages.
    – LaoR
    Commented Oct 31, 2020 at 21:28
15

I had the same problem. I spent two days trying to resolve this problem. I tried reinstalling Visual Studio, cleared Visual Studio cache, uninstalled Visual Studio or setup Windows 10 again => but with no success.

You can try follow steps which worked for me:

  1. Close Visual Studio
  2. Delete the packages folder in Solution folder
  3. Open Visual Studio and rebuild solution

Good luck

1
  • 3
    Just restarting VS did the trick for me, thanks :).
    – jamie
    Commented Sep 28, 2021 at 14:12
9

I just opened Visual Studio "As Administrator" and it worked!

1
  • 3
    Try opening VS as Admin, and it worked. Closed it.. Opened it normally, and still working. No clue what actually changed to fix it. Commented Jul 1, 2022 at 19:36
9

I had the same issue (VS2022) and tried to delete bin en obj folders to resolve it, but that didn't work.

So I force reloaded the dependencies by right-clicking the project that had the problems and selected Load Entire Dependency Tree, which resolved the issue for me!

Load Entire Dependency Tree

4

In my case, I had a project that was still set to build against a beta version of dnx. Changing it to build against the version I was using in the other projects fixed it.

3

I'm guessing a bit here but it looks suspicious that you are using the regular System.Data.* stuff for dnx451 but doesn't have anything to make up for it for dnxcore50. Are you targeting the core edition as well or is it just leftovers from project creation? If you want to target the core edition and still use the old System.Data.* stuff you'll need to wrap anything from those assemblies like this.

#if DNX451
// Do some DNX451 specific stuff here
#endif

And the other way around as well.

#if DNXCORE50
// Do some DNXCORE50 specific stuff here
#endif

But if you aren't thinking about supporting this yet I would simply remove dnxcore50 from the framework list and your errors will probably go away.

6
  • They were just leftovers from project creation, that removed most of the errors thank you. 10 left! The only problem is the errors are still like the ones described above, ie) Predefined type 'System.String' is not defined or imported
    – KidCode
    Commented Jul 4, 2015 at 19:39
  • Those errors shouldn't still be there if you've removed dnxcore50 since they are simple builtin types. In dnxcore you might need to import them (but as soon as you add some other package you'll get the reference implicitly so often you won't add it manually). Are you running the VS 2015 RC (Release Candidate) or are you still using one of the beta versions? Commented Jul 4, 2015 at 19:44
  • I've updated my question to show my file how it stands after your updates. Im also running VS2015 RC.
    – KidCode
    Commented Jul 4, 2015 at 19:53
  • 2
    Ahh, removing the DNXCORE50 reduced the error list to ten, restarting then reduced it to 4.. Removing the System.Runtime reference I added earlier then removed all of the errors... Thank You!
    – KidCode
    Commented Jul 4, 2015 at 20:02
  • 1
    Good to hear. As I said earlier, System.Runtime will almost always be included with another assembly so unless you are making something really basic (ie without even Console.WriteLine) you won't need to include it yourself. Might be something to remember :) Commented Jul 4, 2015 at 20:14
3

In my case, below are the steps I followed;

  1. I removed bin and obj and rebuild. (Didn't work)
  2. Unloaded the project. (It resulted in "Assets file project.assets.json not found. Run a NuGet package restore")
  3. I ran dotnet restore command via Visual Studio 2017 >> Tools >> NuGet Package Manager >> Package Manager Console.
  4. Rebuild solution. (Problem solved)
1
  • 'dotnet restore' worked to me. Commented May 1 at 4:11
2

Took almost 2 days to fix. Tried VS Uninstall\re-install several times but did not work.

What finally worked for me...

Ran VS Installer repair. Due to errors, I had to manually uninstall all instances MS .NET Core SDK - 2.1xxx

Ran VS Installer repair. No errors. Remove bin and obj and rebuild.

Ran dotnet restore command on Package manager console and problem solved.

1
  • Of the solutions listed here, the only one that worked was to do a VS Installer repair. For the obvious reasons (it takes a long time and undoes all customizations) I was avoiding this. But if I had done it up front, it would've saved me the time of trying everything that didn't work. I didn't need to uninstall SDK 2.1xxx, but received an error from the repair process regarding 2.1xxx (which you pretty much expect if you don't uninstall 2.1xxx - the error regarding 2.1 has been reported to Microsoft 10s of thousands of times)
    – unbob
    Commented Jul 25, 2022 at 18:58
1

In my case, this is different. First check System.Runtime reference is added to your project or not. If it is added to your project, please follow the below steps.

  1. Open your Reference from your project and select System.Runtime assembly.
  2. Right click on System.Runtime and select property.
  3. Change the value of SpecificVersion from False to True.

Note : In my Case I am using Visual Studio 2019 and Project Framework Version - 4.6.2

1

VS2019 solution files were placed under the One drive folder, moving to a different folder outside one drive, fixed this.

1

I too received this problem using Visual Studio 2019 and loading a Solution from Google/Git. When my solution was loading, Output tab (from Package Manager), stated that the folder didn't exist: google-api-dotnet-client\NuPkgs\Support. I added the folder NuPkgs then Support within NuPckgs and restarted Visual Studio then all of the projects restored. This fixed my problem.

Check the output tab (from Package Manager) to ensure your projects are restoring successfully.

1

My situation: I am developing in VS 2019 using .NET Framework 4.5. I addded a class library and got the Predefined type 'System.Object' is not defined message on the generated class.

Efforts:

  • Deleted the bin and obj folders, and rebuilt. No change.
  • Ran dotnet restore in the package manager console. Run completed, saying there was nothing for it to do. No change.

My solution: I unloaded the project and reloaded with dependencies. When I rebuilt, the errors went away.

1

In my case the errors are related to a class library project in my solution.

After I re-build that library all errors are disappeared.

Severity Code Description Project File Line Suppression State Error CS0518 Predefined type 'System.Object' is not defined or imported Fabrikafa.CC.Library C:_Repos\Fabrikafa.CC\Fabrikafa.CC.Library\ViewModels\UserViewModel.cs 5 Active

Reading this may help to find a reason behind this error:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0518

1

My theory is its memory related. I regularly hit around 85-90% RAM usage after opening several copies of VS 2019 - it doesn't seem to happen if I just have 1 or 2 copies open. I can be doing nothing and suddenly all my System and System.IO references will get red squiggly lines and intellisense fails to recognise them (though the project builds fine). The only thing that works (until it happens again) is quitting and restarting Visual Studio. Have tried all these which do not work: Unload/reload project Close/reopen solution Wipe bin/obj folders NuGet package cache clear (its not a NuGet package so not sure why this would do anything)

0

For me, the problem occurred when I added a new project to my solution. I re-built the solution and the errors went away.

0

Do these errors only appear when a view (.cshtml) file is open and go away when the file is closed or tabbed away?

MVC5 defines global references for .cshtml razor files to assume at for all views. Check here: project -> Views -> web.config (not to be confused with the project -> web.config file. The system.web.webPages.razor section defines the references:

<configuration>
  ...
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
        Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  ...

However if this project -> views -> web.config file is missing this may be by design, or is not required. I have a projects that when deployed its embedded into a larger top level site which defines these references and so during runtime these errors are cleared. I've learned to ignore and just close the .cshtml files or select non .cshtml file.

It may be possible to grab a copy of the top level views -> web.config file and add it to your solution. I'd think when published the file will be ignored but I am not sure about this behavior.

0

if it helps anybody:

my problem was that the compiler couldn't find System.Index (or something like that)

the error-message didn't really help that much, but then I noticed array[^1] which after some search turns out is just a fancy way to index from the end of the array.

simply changing array[^1] to array[array.Length-1] did the trick.

not the most elegant solution but maybe it helps

0

I looked at the project build order and built only the first project by itself. Once that built, I was able to build the full solution and the errors disappeared.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 13, 2022 at 13:15
0

If you are in VBNET Web, check if you have global.asax with the global references like:

<%@ Application Language="VB" %>
<%@ Import Namespace="System.IO.Compression" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Threading" %>
<script RunAt="server">
</script>
1
  • ASP.NET MVC is used here, not ASP.NET Core.
    – SNBS
    Commented Dec 31, 2022 at 11:21
0

Based on most popular answer, if you have multiple projects in one solution, removing bin and obj folders one by one takes a lot of time, instead execute the following code snippet in Powershell to delete all of them at once.

function Process-Dir {
    param ($Path)
    $Items = Get-ChildItem -Path $Path
    $Items | ForEach {
        $Item = $_

        $ItemPath = Join-Path -Path $Path -ChildPath $Item -Resolve

        # Write-Host -Object "Processing $($ItemPath)"

        if ("$($Item)" -eq "bin" -or "$($Item)" -eq "obj") {
            Remove-Item $ItemPath -Recurse -Force
            Write-Host -Object "Removed  $($ItemPath)"
        }
        elseif (Test-Path -Path $ItemPath -PathType Container) {
            Process-Dir $ItemPath
        }
    }
}

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

Process-Dir $scriptPath

Hope that will work for you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.