ASP.NET MVC Razor Errors After Fresh Installation

2019-07-13 02:54发布

问题:

I recently got a new laptop and did a fresh installation of Visual Studio 2013 Professional. After installation, the first thing I did was to make sure that Nuget was set to automatically download missing packages.

I'm now trying to work on the same large solution I was using before getting my new laptop, but I'm getting some strange errors that I haven't been able to resolve after many hours of research.

The most obvious symptom of the issue is that I see many errors on Razor views. They are these types of errors:

  • Feature 'lambda expression' cannot be used because it is not part of the ISO-2 C# language specification

  • Feature 'anonymous types' cannot be used because it is not part of the ISO-2 C# language specification

  • Feature 'implicitly typed local variable' cannot be used because it is not part of the ISO-2 C# language specification

  • Feature 'extension method' cannot be used because it is not part of the ISO-2 C# language specification

Another symptom is that if I try to add a new view, none of the MVC items are available: for example, no .cshtml items. However, if I create a new solution, these items are available as expected.

I updated to Visual Studio 2013 Update 3 along with the fresh installation. The solution uses MVC 4 and Razor 2. I've checked the web.config and see nothing wrong - the version of Razor and MVC I want are correct.

Despite these errors, my project builds successfully. After the build, all of the 'implicitly typed local variable' errors disappear for a few minutes, then return. Another person is working on the same solution on an old laptop and is not experiencing these issues.

I'm hoping somebody can help. I need to resolve this issue because the lack of useful Intellisense is hurting my productivity.

回答1:

I still have my old laptop, so I compared the settings and file structure on my old machine. The file structure was very different. I was not using the same root directory on the new machine. I copied the files from my old machine and got TFS binding errors when I opened the old version of the solution. I did some research to figure out how to fix the bindings, which led me to managing the workspaces. I removed the existing workspace and added a new one with the correct paths to the solutions on my laptop. A source control box started frantically copying files. When it was finished, I had a lot of conflicts that said "A non version controlled file or or writable file by the same name already exists..." I chose to overwrite the local file for each one, then closed Visual Studio and reloaded the project. Problem solved.

I believe that the root cause of my issue was the invalid workspace. I probably could have avoided copying files from my old machine if I had fixed the workspace first.



回答2:

The reason your receiving that error is because your compiler is more than likely set to a specific version of the C# language. Several of those features are included in later versions. Try the following:

  1. Open the project's Properties
  2. Click the Build tab.
  3. Click Advanced
  4. Modify the Language Version property to default.

Default will accept all valid syntax, which would ensure your utilizing all.

Here is a nice article to ensure Intellisense is enabled and configured correctly, as Visual Studio does have a shortcut that can disable it if you aren't careful.

I believe that is what your looking for, if not let me know with some more details and I'll attempt to help.