Razor 视图没有看到 System.Web.Mvc.HtmlHelper [英] Razor Views not seeing System.Web.Mvc.HtmlHelper

查看:31
本文介绍了Razor 视图没有看到 System.Web.Mvc.HtmlHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级到 MVC4.我已按照 http://www.asp.net/whitepapers/mvc4 上的说明进行操作-release-notes#_Toc303253806 但在我的 Razor 视图和布局中,我有像

I am in the process of upgrading to MVC4. I have followed the instructions at http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 but in my Razor views and layouts I have errors like

  • 'System.Web.WebPages.Html.HtmlHelper' 不包含定义对于 'BeginRouteForm' 并且没有扩展方法 'BeginRouteForm'接受类型的第一个参数可以找到System.Web.WebPages.Html.HtmlHelper"(您是否缺少using 指令或程序集引用?)

我也有类似的错误:

  • 当前上下文中不存在名称Viewbag"

当我将鼠标悬停在@Html 上时,我可以看到它的类型是 System.Web.WebPages.Html.HtmlHelper 而不是 System.Web.Mvc.HtmlHelper

When I hover over @Html I can see it is of type System.Web.WebPages.Html.HtmlHelper not System.Web.Mvc.HtmlHelper

不太确定还有哪些其他信息是相关的,但这就是我所在的位置:

Not really sure what other information is pertinent but this is where I am:

  • 我还没有将 sln 中的类库等更新到 .net 4.5.
  • 该项目是在 VS2010 中创建的,但我正在执行迁移VS2012
  • 该项目在 VS2012、.Net 4.0、MVC 3 下打开并运行正常

感谢任何指点.

web.config 中的所有引用和文件都升级为:

All references and files in web.config upgraded to:

  • System.Web.Mvc,版本=4.0.0.0
  • System.Web.WebPages,版本=2.0.0.0
  • System.Web.Helpers,版本=2.0.0.0
  • System.Web.WebPages.Razor,版本=2.0.0.0

编辑(2):在我的/views/web.config(或/views/shared/web.config,如果我尝试下面的@Paul 解决方案)中,我有我自己的基类型,它继承自 System.Web.Mvc.WebViewPage,包含库中的引用已更新为 MVC4,转到定义会将我带到 MVC4 dll.

Edit(2): In my /views/web.config (or /views/shared/web.config if I try @Paul 's solution below) in the element, I have my own base type which inherits from System.Web.Mvc.WebViewPage , the references in the containing library have been updated to MVC4 and go to definition takes me to the MVC4 dll.

新版本在这方面有什么变化吗?我在发行说明中看不到任何相关内容.

Has anything changed in this area on the new release? I couldn't see anything pertinent in the release notes.

推荐答案

我在使用 Web 应用程序时遇到了这个问题 - 我的 .cshtml 文件卡在了 System.Web.WebPages 上.WebViewPage 基类,当我需要 System.Web.Mvc.WebViewPage 时.

I ran into this issue with a Web Application - my .cshtml files got stuck on the System.Web.WebPages.WebViewPage base class when I needed the System.Web.Mvc.WebViewPage.

首先,确保您的 ~/Views/web.config 文件具有正确的 pageBaseType.就我而言,我设置为 System.Web.Mvc.WebViewPage.

First, ensure your ~/Views/web.config file has the correct pageBaseType. In my case, I set to System.Web.Mvc.WebViewPage.

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <!-- ... -->
    </pages>
  </system.web.webPages.razor>
</configuration>

然后,重要的是,有些人发现如果上述内容已经很好,这是关键:

Then, importantly, some people have found this is key if the above is already good:

  1. 清理解决方案
  2. 卸载有问题的项目
  3. 删除 Visual Studio 在项目旁边生成的 .user 文件.
  4. 重新加载有问题的项目
  5. 构建解决方案

对于 VS2015.user.sln 文件已移动到 .vs 隐藏文件夹中在 .sln 文件旁边创建.但是,从下面的评论中,错误消息向我暗示该工具完全使用了错误的 MVC 版本,删除此文件夹并不能解决问题.据我所知,没有已知的解决方案.

For VS2015, the .user and .sln files have moved to the .vs hidden folder that is created next to the .sln file. However, from comments below, the error messages imply to me that the tooling is using the wrong version of MVC entirely, and deleting this folder does not fix the issue. To my knowledge, there is not a known solution.

这篇关于Razor 视图没有看到 System.Web.Mvc.HtmlHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆