Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore'

I'm getting this error in Visual Studio, when I use @Html.Sitecore:

'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore' and the best extension methods overload 'Sitecore.Mvc.HtmlHelperExtensions.Sitecore(System.Web.Mvc.HtmlHelper)' has some invalid arguments.`

However, once deployed it does run without any problems.

I'm using Sitecore 7.2 with MVC 5.1.

enter image description here

Articles that I've read that deal with similar error messages, talk about the system.web.webPages.razor section of the Views folder web.config file. This is how it appears in my solution.

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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.Routing" />
  </namespaces>
</pages>

Originally I thought I'd just need to restart VS, but that didn't work. Does anyone have any suggestions.

EDIT This is the standard Sitecore 7.2 installation, and so has the following binding redirects:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.1.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
  </dependentAssembly>

Also when looking at the output from visual studio there is an additional error that I didn't spot before:

Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'

EDIT 2

I no longer think this is a Sitecore MVC issue. I get a similar error with @Html.ActionLink("xxx", "xxx"). Other people have seen this issue in VS2012, but I'm using 2013.

EDIT 3

There are now quite a few answers to this question. I recommend trying each of them as it seems there are numerous reasons this error might occur.

like image 723
Martin Davies Avatar asked Jun 10 '14 17:06

Martin Davies


3 Answers

I have the same problem a while ago. We found out that we are missing the web.config inside /views folder. we copy a web.config from my other projects' /views and it solved the issue.

like image 185
Borj Avatar answered Oct 07 '22 22:10

Borj


Try This, then try to restart visual studio.

<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="Sitecore.Mvc" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

You forgot to add <add namespace="Sitecore.Mvc" /> so that intellisense can pick it up in VS.

like image 43
Ahmed Okour Avatar answered Oct 07 '22 20:10

Ahmed Okour


I have now resolved this issue. I simply had to install Update 2 of Visual Studio 2013. How frustrating.

Thanks to StriplingWarrior and Ahmed Okour for your useful advice.

like image 44
Martin Davies Avatar answered Oct 07 '22 21:10

Martin Davies