Martin Davies
Martin Davies

Reputation: 4456

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

Upvotes: 17

Views: 33782

Answers (8)

Ravindra
Ravindra

Reputation: 1

I have added Sitecore.Mvc dll in references with copy local to true and added below tag in view's web.config under namespaces tag, then my problem is resolved.

Upvotes: -1

Chamara
Chamara

Reputation: 209

  <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" />
    <add namespace="Kendo.Mvc.UI" />
    <add namespace="Telerik.Reporting" />
  </namespaces>

Upvotes: 0

Rajan
Rajan

Reputation: 61

I have used the below line at the top and the issue has been resolved.

@inherits System.Web.Mvc.WebViewPage

Upvotes: 6

Paul George
Paul George

Reputation: 1817

Sometimes its the silly things. Check you're not doing something like

@Html.Sitecore("placeholdername")   // no method

or

@Html.Sitcore.Placeholder("placeholdername") // Missing parenthesis

when you should be doing

@Html.Sitecore().Placeholder("placeholdername") // this works

Upvotes: 0

Borj
Borj

Reputation: 43

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.

Upvotes: 19

Martin Davies
Martin Davies

Reputation: 4456

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.

Upvotes: 4

Ahmed Okour
Ahmed Okour

Reputation: 2422

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.

Upvotes: 17

StriplingWarrior
StriplingWarrior

Reputation: 156564

You say you're using MVC 5, but you're referencing version 3 in the code you provided. Here's what mine says:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Try checking through your entire web.config file (or all the web.config files if you have multiple Areas) and making sure that all the versions are set right. For MVC 5, System.Web.WebPages should be on version 2 and System.Web.WebPages.Razor should be on version 3.

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

...

  <assemblies>
    <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>

Upvotes: 3

Related Questions