16

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.

Martin Davies
  • 4,436
  • 3
  • 19
  • 40
  • Here was solved similar problem: [Stackoverflow solutions](http://stackoverflow.com/questions/12041633/razor-views-not-seeing-system-web-mvc-htmlhelper) – paweln66 Jan 07 '16 at 15:25

8 Answers8

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.

Borj
  • 43
  • 7
  • The `/views/web.config` file was already present in my case, but thanks. – Martin Davies Jun 20 '14 at 08:05
  • 1
    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. – Steve Newstead Nov 10 '15 at 10:01
15

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.

Ahmed Okour
  • 2,392
  • 1
  • 16
  • 31
  • 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. – StriplingWarrior Jun 10 '14 at 22:54
  • I did try adding it to the config too, but it didn't help. But thanks for the suggestion – Martin Davies Jun 11 '14 at 07:47
  • Then this should a visual studio issue, Not Sitecore, try this on different machine – Ahmed Okour Jun 11 '14 at 09:12
  • 1
    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:- http://www.seanholmesby.com/fixing-visual-studio-intellisense-in-sitecore-mvc-views/ – Sean Holmesby Aug 01 '14 at 13:35
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.

Martin Davies
  • 4,436
  • 3
  • 19
  • 40
  • I summarised this in a blog post: http://sitecoreskills.blogspot.com/2014/06/preparing-sitecore-72-and-mvc-in-visual.html – Martin Davies Jun 19 '14 at 19:08
  • 1
    Yes this solution works fine also for me. I spend hours on investigating web.config files but finally Update 5 solve the problem. – paweln66 Jan 08 '16 at 15:12
4

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

@inherits System.Web.Mvc.WebViewPage
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Rajan
  • 41
  • 1
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>
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 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. – Martin Davies Jun 10 '14 at 18:45
  • [This guy](http://kamsar.net/index.php/2013/12/using-mvc-5-with-sitecore-7-1/) apparently also recommends commenting out the razor ``. Have you done that? – StriplingWarrior Jun 10 '14 at 18:49
  • Tried that too, but no luck. Think this might be one for Sitecore Support. – Martin Davies Jun 10 '14 at 20:24
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
Paul George
  • 1,788
  • 16
  • 33
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>
Chamara
  • 199
  • 2
  • 3
  • 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 mention – Chamara Oct 07 '16 at 06:46
-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.

Ravindra
  • 1
  • 1