asp.net-mvc “System.Web.Webpages.Html.Htmlhelper”不包含“Sitecore”的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24147846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore'
提问by Martin Davies
I'm getting this error in Visual Studio, when I use @Html.Sitecore:
当我使用@Html.Sitecore以下命令时,我在 Visual Studio 中收到此错误:
'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.`
“System.Web.Webpages.Html.Htmlhelper”不包含“Sitecore”的定义,并且最好的扩展方法重载“Sitecore.Mvc.HtmlHelperExtensions.Sitecore(System.Web.Mvc.HtmlHelper)”有一些无效的参数。`
However, once deployed it does run without any problems.
但是,一旦部署,它就可以毫无问题地运行。
I'm using Sitecore 7.2 with MVC 5.1.
我将 Sitecore 7.2 与 MVC 5.1 一起使用。


Articles that I've read that deal with similar error messages, talk about the system.web.webPages.razorsection of the Viewsfolder web.configfile. This is how it appears in my solution.
我读过的处理类似错误消息的文章,讨论system.web.webPages.razor了Views文件夹web.config文件的部分。这就是它在我的解决方案中的显示方式。
<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.
最初我以为我只需要重新启动 VS,但这没有用。有没有人有什么建议。
EDITThis is the standard Sitecore 7.2 installation, and so has the following binding redirects:
编辑这是标准的 Sitecore 7.2 安装,因此具有以下绑定重定向:
<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:
此外,在查看 Visual Studio 的输出时,还有一个我之前没有发现的额外错误:
Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'
Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'
EDIT 2
编辑 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.
我不再认为这是 Sitecore MVC 的问题。我收到类似的错误@Html.ActionLink("xxx", "xxx")。其他人在 VS2012 中看到过这个问题,但我使用的是 2013。
EDIT 3
编辑 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.
这个问题现在有不少答案。我建议尝试每一个,因为似乎有很多原因可能会发生此错误。
采纳答案by Martin Davies
I have now resolved this issue. I simply had to install Update 2 of Visual Studio 2013. How frustrating.
我现在已经解决了这个问题。我只需要安装 Visual Studio 2013 的 Update 2。多么令人沮丧。
Thanks to StriplingWarrior and Ahmed Okour for your useful advice.
感谢 StriplingWarrior 和 Ahmed Okour 的有用建议。
回答by Borj
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.
我前一段时间也有同样的问题。我们发现 /views 文件夹中缺少 web.config。我们从我的其他项目的 /views 复制了一个 web.config,它解决了这个问题。
回答by Ahmed Okour
Try This, then try to restart visual studio.
试试这个,然后尝试重新启动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.
您忘记添加 <add namespace="Sitecore.Mvc" />以便智能感知可以在 VS 中提取它。
回答by StriplingWarrior
You say you're using MVC 5, but you're referencing version 3 in the code you provided. Here's what mine says:
您说您使用的是 MVC 5,但您在提供的代码中引用了版本 3。这是我的说法:
<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.WebPagesshould be on version 2 and System.Web.WebPages.Razor should be on version 3.
尝试检查整个 web.config 文件(如果有多个区域,则检查所有 web.config 文件)并确保所有版本都设置正确。对于 MVC 5,System.Web.WebPages应该在版本 2 上,System.Web.WebPages.Razor 应该在版本 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>
回答by Rajan
I have used the below line at the top and the issue has been resolved.
我在顶部使用了以下行,问题已解决。
@inherits System.Web.Mvc.WebViewPage
回答by Chamara
<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>
回答by Paul George
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
回答by Ravindra
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.
我在引用中添加了 Sitecore.Mvc dll,将本地复制到 true 并在名称空间标签下的视图 web.config 中添加了下面的标签,然后我的问题就解决了。

