22

I have a problem, which I cannot solve. I can't open every cshtml-File in a Visual Studio c# mvc project (whatever which project i tried). I get the following error Msg (I try to translate to English):

Object reference not set to an instance to an object

Google says, it may be a NullReferenceExeception. But it has nothing to do with my code, because first I can execute the project successfully and second the error occurs in every project (since today).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Maermot
  • 245
  • 1
  • 3
  • 8
  • i know what a NullReference is... But you should read my question, before duplicate it. My code works for sure, as i said, the problem occurs for EVERY project i open. And i cannot open a cshtml file!! That doesnt make sense at all. Its like i have a NullReference to my linked cshtml Files, but i deleted all from my project and reinsert all. Nothing helps – Maermot Dec 08 '15 at 12:33
  • In VS, right click on .cshtml file then Open With... Post here what is your default program to open this file – skalinkin Dec 08 '15 at 13:08
  • HTML-Editor is my standard (ah if i open the file with html-editor(web forms) it works!! But how can i fix it for the normal HTML Editor??) – Maermot Dec 08 '15 at 13:23
  • looks like the issue with VS. try to reinstall in repair mode. if does not help run visual studio with logging flag and see logs for details of the error. https://msdn.microsoft.com/en-us/library/ms241272.aspx – skalinkin Dec 08 '15 at 16:28
  • Also, do you have any addons, pluggins etc. installed which related to html editing? – skalinkin Dec 08 '15 at 16:30

8 Answers8

19

I just experienced the same issue. It just came out of nowhere, as you described. I even went through a 2 hour repair. That didn't fix it.

However, a coworker suggested this fix.

Visual Studio 2015 not syntax highlighting razor nor Intellisense

I had the same problem, the only fix so far is by resetting all user data (C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /ResetUserData).

Now I am able to use the real default editing and I get good syntax highlighting again.

Community
  • 1
  • 1
mac10688
  • 2,145
  • 2
  • 24
  • 37
  • 4
    Yikes. What a horrible, heavy handed solution. BUT, it worked. – Jon Sagara Dec 18 '15 at 21:23
  • 1
    Saved me bacon. This solution worked great. I just wish I had tried it before paying the 3 hour repair tax. – Kevin R. Feb 19 '16 at 00:17
  • 1
    I'm having the same issue now. I didn't have it after installing ASP.NET RC1 like some did. Mine appeared after installing Visual Studio 2015 Update 2. Up to this point, nothing I've done fixed the issue. I reset user data. Removed the contents of ComponentModelCache folder. I even ran a repair on Visual Studio which took about 3 hours and the problem is still there. At this point, I don't even know what to do. – Sam Jun 15 '16 at 03:23
  • I just ran into this problem after installing Visual Studio 2015 Update 3 and this fixed it. – Gordon Glas Jun 30 '16 at 15:34
  • 1
    Elaborating on Jon Sagara's warning, this will wipe out your saved login credentials, saved TFS servers, and probably other things. It did work for me, though. For those of you unfamiliar with how to run mac10688's command line fix, you can open a command prompt, key "cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\" (without the quotes), hit enter, key "devenv.exe /ResetUserData" (again, no quotes), then hit enter. Open VS, re-log in, then hopefully your problem is fixed like mine! – David Alan Condit Aug 31 '18 at 20:25
  • 1
    For me it was C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE>devenv.exe /ResetUserData – Steven Dec 19 '18 at 18:28
17

The solution that worked for me was from here

  1. Close VS
  2. Delete the content of %LocalAppData%\Microsoft\VisualStudio\15.0\ComponentModelCache
  3. Open VS

As suggested by @jamez14, if your are using a different version of Visual Studio, just change 15.0 in the path to the version you have.

Miro J.
  • 4,270
  • 4
  • 28
  • 49
7

I faced the following issue.

ISSUE: All .cshtml were not opening in visual studio 17 community.

REASON: Accendently shutdown laptop by power off button while files were open in visual studio.

I tried all the above solutions but none worked.

Following solution worked for me.

Repaired Visual Studio using Visual Studio Installer.

TarakPrajapati
  • 479
  • 3
  • 12
4

I had the same problem and the issue turned out that I had disabled Razor Language Service Pack.

To re-enable go to :

Tools > Extensions and Updates > search for Razor Language Package and enable. Close VS and reopen.

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Deathstalker
  • 794
  • 10
  • 8
2

Running the in console the command: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /ResetUserData should fix your problem as it resets all user data

alecellis1985
  • 131
  • 2
  • 15
1

This worked for me: Visual Studio 2015 Broken Razor Intellisense Didn't reset my preferences or caused other side-effects.

Community
  • 1
  • 1
starnovsky
  • 76
  • 2
1

It is caused by the version of Razor when updates by NuGet and clean the solution. re-build the project and will open without problems.

1

I had this same thing recently, resetting profiles and the like didn't work, it turned out that I had some dodgy web.config settings:- Removing these lines and re-building the project appeared to solve the issue and I could open cshtml files normally:

<add key="webpages:Version" value="4.0.0.0" />
<add key="webpages:Enabled" value="false" />

I found this solution in a github bug thread that mentions many other potential fixes. It seems to be related to updating your version of ASP.Net on an existing project.

Izzy
  • 1,764
  • 1
  • 17
  • 31