4

I've recently updated an ASP.NET MVC project from v3 to v4, and now my .cshtml Razor views have lost intellisense when edited in Visual Studio. Keywords and helper properties such as @model, @Url, @Html etc. are not being recognised and are producing errors in VS. When ran, the app itself works fine.

I've tried suggested solutions from various online sources, including SO, most of which involve tweaks to the web.config. I've been over them and I'm pretty sure my config settings are correct for MVC 4:

<configuration>
    <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
</configSections>

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.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>

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

<system.web>
    <httpHandlers>
        <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>    
    <pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <controls>
            <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
        </controls>
    </pages>    
</system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
       <remove name="BlockViewHandler"/>
       <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>
</configuration>

Now, I have made some observations which give me an inkling what the problem might be. If I go through my web.config and revert the references to MVC 3, i.e. MVC from version 4.0 to 3.0, and Razor/WebPages from 2.0 to 1.0, my view intellisense comes back (but obviously this leaves me with a non-functioning app).

I think I may possibly have an installation issue and Visual Studio, for whatever reason, is unable to locate the assemblies specified in the web.config for the purposes of Razor intellisense. I installed the ASP.NET MVC 4 reference via NuGet, and I've double-checked that all my referenced assemblies are the correct version. I've tried re-installing the MVC package and I've tried cleaning and rebuilding several times.

Thanks in advance for any suggestions as to how I can get this working. It's really hampering my productivity at the moment.

Lee D
  • 12,551
  • 8
  • 32
  • 34
  • @ I have also have similar problem and reinstalling the MVC framework worked in my case,, – Bhupendra Shukla May 01 '14 at 06:11
  • 1
    Have you tried everything in the official release notes? http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 – deerchao May 05 '14 at 10:10
  • @deerchao - Thanks for the link - it was the changing of the GUID in the .csproj file which fixed the issue. – Lee D May 05 '14 at 11:11
  • Glad it helped. Is there any chance you live in Beijing, China and is looking for a new job? I'm hiring one which I think you qualify based on your stackoverflow profile, if you are interested. – deerchao May 05 '14 at 12:33
  • I'm in the UK, a long way from Beijing i'm afraid! – Lee D May 05 '14 at 14:44
  • @LeeD No worries, I'll wait for the invention of the teleportation machine. – deerchao May 05 '14 at 18:04
  • possible duplicate of [MVC Razor view Intellisense broken in VS 2013](http://stackoverflow.com/questions/22832435/mvc-razor-view-intellisense-broken-in-vs-2013) – Chris Moschini Jul 25 '14 at 00:53

2 Answers2

0

I think you need the System.Web.Helpers namespace and possibly System.Web.WebPages:

Change this:

<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>

to:

<namespaces>
  <add namespace="System.Web.Helpers" />
  <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" />
  <add namespace="System.Web.WebPages" />
</namespaces>
SkyBlues87
  • 1,196
  • 9
  • 17
0

The solution which worked for me (taken from the release notes at http://www.asp.net/whitepapers/mvc4-release-notes):

  • In Solution Explorer, right-click the project name and then select Unload Project. Then right-click the name again and select Edit ProjectName.csproj.
  • Locate the ProjectTypeGuids element and replace {E53F8FEA-EAE0-44A6-8774-FFD645390401} with {E3E379DF-F4C6-4180-9B81-6769533ABE47}.
Lee D
  • 12,551
  • 8
  • 32
  • 34