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

8

17

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.

Intracellular answered 10/6, 2014 at 17:58 Comment(1)
Here was solved similar problem: Stackoverflow solutionsTrossachs
I
4

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.

Intracellular answered 11/6, 2014 at 22:59 Comment(2)
I summarised this in a blog post: sitecoreskills.blogspot.com/2014/06/…Intracellular
Yes this solution works fine also for me. I spend hours on investigating web.config files but finally Update 5 solve the problem.Trossachs
C
19

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.

Cryotherapy answered 10/6, 2014 at 17:58 Comment(2)
The /views/web.config file was already present in my case, but thanks.Intracellular
I'd also popped a razor file in the layouts folder which was causing my beef. So to be clear you need to either keep all razor files in the views folder OR add a web.config wherever you plonk any razor views.Holyhead
P
17

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.

Pettigrew answered 10/6, 2014 at 22:44 Comment(4)
True, but he does have @using SiteCore.Mvc at the top of his razor view, so I don't think this will fix the problem.Grantinaid
I did try adding it to the config too, but it didn't help. But thanks for the suggestionIntracellular
Then this should a visual studio issue, Not Sitecore, try this on different machinePettigrew
For VS 2013, follow Martin's answer about updating to Update 2. You will also need to have the appropriate App_Config files within your project directory (not necessarily included in your project though) Here's my post on it:- seanholmesby.com/…Fonzie
L
6

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

@inherits System.Web.Mvc.WebViewPage
Littles answered 20/9, 2016 at 10:55 Comment(0)
I
4

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.

Intracellular answered 11/6, 2014 at 22:59 Comment(2)
I summarised this in a blog post: sitecoreskills.blogspot.com/2014/06/…Intracellular
Yes this solution works fine also for me. I spend hours on investigating web.config files but finally Update 5 solve the problem.Trossachs
G
3

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>
Grantinaid answered 10/6, 2014 at 18:22 Comment(3)
I should have mentioned that this is a standard Sitecore 7.2 installation, and so has assembly binding redirects for System.Web.WebPages.Razor and System.Web.Mvc (See my updates to the question.). However. I did follow your suggestions, but unfortunately they didn't make a difference.Intracellular
This guy apparently also recommends commenting out the razor <sectionGroup>. Have you done that?Grantinaid
Tried that too, but no luck. Think this might be one for Sitecore Support.Intracellular
B
0
  <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>
Bolter answered 10/6, 2014 at 17:58 Comment(1)
First go to the (reference-->Add reference) and Add kendo.Mvc.dll then after go to the view file and edit webconfig file as above mentionBolter
H
0

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
Hofstetter answered 11/1, 2016 at 17:7 Comment(0)
C
-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.

Centre answered 5/12, 2016 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.