2

I have no intellisense in my cshtml. I have in my web.config (views folder):

   <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.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.Routing" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="EO2.Models" />
        <add namespace="EO2" />
        <add namespace="EO2.Resources"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

only when adding the @using MyNamespace in my cshtml it works.

I checked all versions numbers as desribed here: MVC Razor view Intellisense broken in VS 2013/2015/2017 Intellisense in razor files (.cshtml) not work

Edit: Problem is in VS or other project files? I have the same problem also in every new solution.

Community
  • 1
  • 1
daniel
  • 34,281
  • 39
  • 104
  • 158

3 Answers3

0

Tools->Import/Export Settings->Reset Settings

did it for me

daniel
  • 34,281
  • 39
  • 104
  • 158
  • You don't need to reset your VS settings, closing and reopening your cshtml file is enough. – onur Aug 26 '17 at 09:47
0

tldr; Try switching build configurations and also deleting obj


I haven't had this problem for YEARS and suddenly started getting it.

>------ Build started: Project: MyMVC, Configuration: Debug Any CPU ------
6>S:\TFS\RRStore\RRStore.Main\MyMVC\obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Category\SearchPanel\Search.cshtml.cs(22,26,22,27): error CS0116: A namespace cannot directly contain members such as fields or methods
========== Build: 5 succeeded, 1 failed, 9 up-to-date, 0 skipped ==========

Wait a minute! I'm doing a DEBUG build and it is complaining about obj\Release

So I deleted obj completely and got this error:

15>------ Rebuild All started: Project: MicroPedi_MVC, Configuration: Debug Any CPU ------
15>...RazorGenerator.MsBuild.targets(44,9): error : Could not find a part of the path 'S:\TFS\RRStore\RRStore.Main\My_MVC\obj\Release\Package\PackageTmp\Areas\Admin\Views\CallCenter\Index.cshtml'.
========== Rebuild All: 14 succeeded, 1 failed, 0 skipped ==========

What!! That folder doesn't even exist (obj\Release)!

So I switched to Release configuration. Everything built. Now if I recall this doesn't prove anything because I think I'm using pre-compiled views - tbh I forget how it works.

Anyway THEN I switched back to Debug configuration.

Everything suddenly worked!


If that doesn't help - just try some basic detective work and assume nothing is working!

I found it was building fine on my server and I had made no changes to my web.config in a long long time.

  • Try making an inconsequential change to your file (to make sure the codegen is updating). eg. add a comment, classname or change some text
  • Find the generated file which will be something like \obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Products.cshtml.cs
  • See if your trivial change is reflected there
  • If not delete everything in \obj\CodeGen\... or probably in obj
  • Build your project

Another possibility:

I had recently installed .NET Framework 1.1 (to be able to extract a 3rd party library I needed).

I uninstalled it anyway, but not clear if this was the reason anything got screwed up. Suspicious though - especially since 1.1. always liked to mess with IIS.

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
0

I would suggest to:

  1. Create a new MVC5 vs2019 project.
  2. Navigate to web.config that resides under the views folder
  3. Replace the 'broken' project web.config with the one from point 2
  4. Fix missing project references

This should fix the error and make build possible again.

Luca Ghersi
  • 3,261
  • 18
  • 32